Reputation: 467
I have some doubts regarding how does @RequestMapping and @RequestBody actually woks.I have a code which is as follows:
@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...
Upvotes: 7
Views: 13094
Reputation: 7111
The Spring documentation explains it very well, for @RequestMapping
You use the @RequestMapping annotation to map URLs such as /appointments onto an entire class or a particular handler method.
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.
As for @ResponseBody
it says:
This annotation can be put on a method and indicates that the return type should be written straight to the HTTP response body
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()
Upvotes: 3
Reputation: 1727
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:
http://localhost:8080/war/method.do
the application server and spring will call the getActionResponse method on your class.
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".
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
Upvotes: 1
Reputation: 31754
So simply what it does is that, as per the 'url mapping' expressed in the xml file of your web.xml
It appends "method.do" to it
So for example: My application name is `Hello' and below is my 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
@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
Upvotes: 1