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…
The Spring documentation explains it very well, for @RequestMapping
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 methodgetActionResponse(HttpServletRequest,HttpServletResponse)and Spring will bind the parameters automatically.As for
@ResponseBodyit says: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:
See ServletResponse#getWriter() or ServletResponse#getOutputStream()