Difference between parameters taken by @RestContro

2019-07-19 17:49发布

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")

2条回答
地球回转人心会变
2楼-- · 2019-07-19 18:24

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.

查看更多
祖国的老花朵
3楼-- · 2019-07-19 18:25

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 {
    ...
}
查看更多
登录 后发表回答