Difference between parameters taken by @RestController and @RequestMapping annotations?

╄→гoц情女王★ 提交于 2019-12-12 19:22:28

问题


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.



来源:https://stackoverflow.com/questions/48891083/difference-between-parameters-taken-by-restcontroller-and-requestmapping-annot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!