Difference between parameters taken by @RestContro

2019-07-19 17:51发布

问题:

What does the parameter in @RestController("/path/..") do? Does it not set the base path like @RequestMapping("/path/.."). What is the difference?

@RestController("base-path")

回答1:

In case of @RestController the parameter value depicts the component name or bean name, whereas in @RequestMapping the value parameter is used to specify the path. Both are used for different purpose.

If you want to specify request URI path on controller class name use @RequestMapping annotation with @RestController. Something like this:

@RequestMapping("/my-path")
@RestController
class MyController {
    ...
}


回答2:

Taken from the Spring Documentation:

  • @RestController -

is known as a stereotype annotation. It provides hints for people reading the code, and for Spring, that the class plays a specific role. ... so Spring will consider it when handling incoming web requests.

  • @RequestMapping -

annotation provides “routing” information. It is telling Spring that any HTTP request with the path “/” should be mapped to the home method. The @RestController annotation tells Spring to render the resulting string directly back to the caller.