java 谁能解释@RequestMapping 和@RequestBody 是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13487964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can anyone explain how does @RequestMapping and @RequestBody works?
提问by Mr. Singthoi
I have some doubts regarding how does @RequestMapping and @RequestBody actually woks.I have a code which is as follows:
我对@RequestMapping 和@RequestBody 实际上如何工作有一些疑问。我有一个代码如下:
@Controller
public class CoreController {
@Autowired
LoggerExtension log;
@Autowired
DoService doService;
@RequestMapping(value="/method.do")
public @ResponseBody String getActionResponse(HttpServletRequest request,HttpServletResponse response){
String action = request.getParameter("action");
String gender = request.getParameter("gender");
String language = request.getParameter("language");
if("getLanguage".equalsIgnoreCase(action)){
returnResponse = doService.getUserLanguage(msisdn);
}
}
return returnResponse;
}
I want to know how does the above code works? Please help me to clear this concepts...
我想知道上面的代码是如何工作的?请帮我清除这些概念...
采纳答案by ElderMael
The Spring documentation explains it very well, for @RequestMapping
对于@ RequestMapping,Spring 文档对此进行了很好的解释
You use the @RequestMapping annotation to map URLs such as /appointments onto an entire class or a particular handler method.
您可以使用 @RequestMapping 批注将诸如 /appointments 之类的 URL 映射到整个类或特定处理程序方法。
In your specific case, @RequestMapping(value="/method.do")
means that a http request (in any method) to the URI /method.do
(e.g. http://myserver.com/app/method.do
) will be handled by the annotated method getActionResponse(HttpServletRequest,HttpServletResponse)
and Spring will bind the parameters automatically.
在您的特定情况下, @RequestMapping(value="/method.do")
意味着对 URI /method.do
(例如http://myserver.com/app/method.do
)的 http 请求(以任何方法)将由带注释的方法处理getActionResponse(HttpServletRequest,HttpServletResponse)
,Spring 将自动绑定参数。
As for @ResponseBody
it says:
至于@ResponseBody
它说:
This annotation can be put on a method and indicates that the return type should be written straight to the HTTP response body
这个注解可以放在一个方法上,表明返回类型应该直接写到 HTTP 响应体中
In your specific case, this means that the returned string of the method annotated will be written to the response output stream or the writter like if you were calling something like this:
在您的特定情况下,这意味着被注释的方法的返回字符串将被写入响应输出流或编写器,就像您调用这样的东西一样:
String result = getActionResponse(request, response)
response.getWriter().print( result ); //Suppose result is "en_US" or something
See ServletResponse#getWriter()or ServletResponse#getOutputStream()
请参阅ServletResponse#getWriter()或ServletResponse#getOutputStream()
回答by Alon Bar David
What your looking at is the way to create a WebService using Spring.
The @RequestMapping annotation maps the path in the value to the method.
So (Assuming your server is setup on localhost:8080 and your war is named 'context') if you call the following url:
您所看到的是使用 Spring 创建 WebService 的方法。
@RequestMapping 注释将值中的路径映射到方法。
因此(假设您的服务器设置在 localhost:8080 上并且您的War名为“context”),如果您调用以下网址:
http://localhost:8080/war/method.do
the application server and spring will call the getActionResponse method on your class.
应用程序服务器和 spring 将在您的类上调用 getActionResponse 方法。
normally the return value of getActionResponse would be treated as a url, so if you return the String 'text', the server would redirect to /war/text .
The @ResponseBody annotation tells spring that the returned String should actually be returned as the message of the response, so after you make the call, the server would return a 200 OK response with a message body "text".
通常 getActionResponse 的返回值将被视为一个 url,因此如果您返回 String 'text',服务器将重定向到 /war/text 。
@ResponseBody 注释告诉 spring 返回的 String 实际上应该作为响应的消息返回,因此在您进行调用后,服务器将返回带有消息正文“文本”的 200 OK 响应。
edit: forgot about the basic mapping in the web.xml, see Jatin's answer . so instead of htt'p://localhost:8080/war/method.do it would be htt'p://localhost:8080/war/rest/method.do
编辑:忘记了 web.xml 中的基本映射,请参阅 Jatin 的回答。所以不是 htt'p://localhost:8080/war/method.do 而是 htt'p://localhost:8080/war/rest/method.do
回答by Jatin
So simply what it does is that, as per the 'url mapping' expressed in the xml file of your web.xml
所以简单地说,它的作用是,根据 web.xml 的 xml 文件中表示的“url 映射”
It appends "method.do" to it
它将“method.do”附加到它
So for example: My application name is `Hello' and below is my web.xml
例如:我的应用程序名称是“Hello”,下面是我的 web.xml
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Now your url for your mentioned will be localhost/rest/method.do
现在你提到的网址将是 localhost/rest/method.do
@RequestMapping
is a way of telling the handling servlet, that the response of the strign is the actual response. Ideally you will have a view to which the output will be forwarded.
But in this case your response is the view, hence the annotation @RequestMapping
@RequestMapping
是一种告诉处理 servlet 的方法,strign 的响应是实际响应。理想情况下,您将看到输出将转发到的视图。但在这种情况下,您的响应是视图,因此注释@RequestMapping