How to return a simple xml string from a form post in Spring MVC
I've got a simple http POST action in Spring MVC and I don't need to return a full web page. Instead I just need to return an xml string (for example)
true
But 开发者_开发问答when I exec the action below my client is getting a 404
@RequestMapping(value = "/updateStuffAjaxStyle.do", method = RequestMethod.POST)
public String updateStuffAjaxStyle(HttpServletRequest request, HttpServletResponse response) {
//..do something w/ the inputs ...
return "<valid>true</valid>";
}
Is it possible to return a simple xml string like this w/out having to define a ton of bean defs?
I believe you can do this by annotating your method return type with the @ResponseBody
annotation like so:
@RequestMapping(value = "/updateStuffAjaxStyle.do", method = RequestMethod.POST)
public @ResponseBody String updateStuffAjaxStyle(HttpServletRequest request,
HttpServletResponse response) {
//..do something w/ the inputs ...
return "<valid>true</valid>";
}
Yes, it is. But not by returning the String from your method, but by writing it to the HttpServletResponse.getWriter()
and changing the method signature to return void
(this way, Spring will know that you'll handle the response yourself).
To grab the servlet response writer, simply add one extra argument of type java.io.Writer
to your method, and Spring will provide you with the proper reference.
精彩评论