@RequestMapping (value = "/produces", produces = "Application/json"): Indicates that the function processing method will produce JSON-formatted data, which is matched according to the accept in the request header, such as the request Header " Accept:application/json "can be matched;
@RequestMapping (value = "/produces", produces = "Application/xml"): Indicates that the function processing method will produce XML-formatted data, which is matched according to the accept in the request header, such as the request Header " Accept:application/xml "can be matched.
This method is more indicative of your purpose than using @requestmapping's "headers =" Accept=application/json ".
Server controller code detailed cn.javass.chapter6.web.controller.consumesproduces.ProducesController;
The client code is similar to the client in the previous content-type, see Producescontroller.java code.
When you have the following accept headers:
①accept:text/html,application/xml,application/json
The produces matching is performed in the following order ①text/html②application/xml③application/json
②accept:application/xml;q=0.5,application/json;q=0.9,text/html
The produces matching is performed in the following order ①text/html②application/json③application/xml
The q parameter is the mass factor of the media type, the higher the priority (from 0 to 1)
③accept:*/*,text/*,text/html
The produces matching is performed in the following order ①text/html②text/*③*/*
That is, the matching rule is: the most explicit priority match.
Code See PRODUCESPRECEDENCECONTROLLER1, ProducesPrecedenceController2, ProducesPrecedenceController3.
Third, the narrowing is covered rather than inherited
such as class-level mappings are @RequestMapping (value= "/narrow", produces= "text/html"), Method-level @requestmapping (produces= "Application/xml" ), the mapping at the method level overrides the class level, so the request header "Accept:application/xml" is successful, and "text/html" will report a 406 error code indicating the unsupported request media type.
See Cn.javass.chapter6.web.controller.consumesproduces.NarrowController.
Only the producer/consumer model is covered, the other uses are inherited, such as headers, params, etc. are inherited.
Iv. relationship of combination use "or"
@RequestMapping (produces={"text/html", "Application/json"}): Will match "accept:text/html" or "Accept:application/json".
V. Questions
Consumption of data, such as JSON data, XML data are the InputStream of our request to read and according to the need to transform themselves into the corresponding model data, more trouble;
The production of data, such as JSON data, XML data is the first to convert the model data to json/xml data, and then output the response stream, it is also more troublesome.
What does @produces mean in spring MVC?