What does @produces mean in spring MVC?

Source: Internet
Author: User

@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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.