Java – Some information about produces attribute inside @RequestMapping in Spring MVC

annotationsjavaspringspring-mvc

in the Spring MVC Showcase example (dowlodable from STS dashboard) I have the following situation.

In my view I have the following link:

        <li>
            <a id="responseCharsetProduce" class="textLink" href="<c:url value="/response/charset/produce" />">@ResponseBody (UTF-8 charset produced)</a>
        </li>

This link generate an HTTP Request towards the URL: "/response/charset/produce"

Ok, this HTTP Request is handleed by the following method of a controller class (my controller class is itself annotated with @RequestMapping(value="/response", method=RequestMethod.GET so this method handled this request):

@RequestMapping(value="/charset/produce", produces="text/plain;charset=UTF-8")
public @ResponseBody String responseProducesConditionCharset() {
    return "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01 (\"Hello world!\" in Japanese)";
}

My doubt is related to the produces element inside the @RequestMapping annotation…

Reading here: http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html

say that: String[] produces The producible media types of the mapped request, narrowing the primary mapping. so it seems that produces say only what media types is produced by the method…in my specific case say that the media type returned by the controller method is textual value…

But reading elsewhere instead I found that the produces attribute excludes HTTP Request with Accepts header incompatible with the media type specified…

So…what exactly do produces attribute inside @RequestMapping?

Best Answer

The "produces" condition indicates what the method will return . If the client didn't specify an Accept header, then nothing prevents the method from returning what it can.

There is are some comments by the blog author pertaining to your question at the url below.

http://spring.io/blog/2011/06/13/spring-3-1-m2-spring-mvc-enhancements