3

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?

2 Answers 2

7

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>";
}
Sign up to request clarification or add additional context in comments.

3 Comments

What if I am wanting to return SVG XML to display in an <img> tag?
I don't see why this should be any different to the above. Just swap out the string above with the SVG XML you want to use.
I had to create a HttpHeaders object and set the content type. See this question that I made.
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.