Content-type explanations in HTTP requests and produce and consumes configuration in spring MVC annotations

Source: Internet
Author: User
Tags class definition json

Introduction: In HTTP requests, we use Content-type every day to specify different formats of request information, but few people have a thorough understanding of how many values are allowed in Content-type, and this will explain content-type available values, as well as the spring How they are used in MVC to map request information.

1. Content-type

MediaType, which is the Internet media type, the Internet medium, or the MIME type, in the HTTP protocol message header, uses Content-type to represent the media type information in the specific request.
[HTML] view plain Copy type format: Type/subtype (;p arameter)? Type main type, any string, such as text, if the * symbol represents all; Subtype subtype, arbitrary string, such as HTML, if the * number represents all; parameter optional, some parameters, such as the Q parameter of the Accept request header, Content-typ     The charset parameter of E. For example: Content-type:text/html;charset:utf-8;

The common media format types are as follows: text/html:html format text/plain: Plain text Format
Text/xml:xml format image/gif:gif Picture format
Image/jpeg:jpg Picture Format
Image/png:png Picture Format

Media format type starting with application: application/xhtml+xml:xhtml format application/xml:xml data format Application/atom+xml:atom X ML aggregation format
Application/json:json data Format application/pdf:pdf format
Application/msword:word Document Format application/octet-stream: binary stream data (such as common file downloads) application/x-www-form-urlencoded: <f ORM enctype= "" > The default enctype,form form data is encoded in Key/value format sent to the server (form default submission data format)

Another common media format is when uploading files: multipart/form-data: When you need to upload a file in a form, you need to use that format

These are some of the content-type content formats that we often use in our daily development.

2. About the use of Content-type type information in Spring MVC

First, let's look at the class definition in requestmapping:
[HTML]View Plain Copy @Target ({elementtype.method, elementtype.type}) @Retention (retentionpolicy.runtime) @Documented @Map         Ping public @interface requestmapping {string[] value () default {};         Requestmethod[] Method () default {};         String[] Params () default {};         String[] Headers () default {};         String[] Consumes () default {};   String[] produces () default {}; } value: Specifies the actual address of the request, such as/action/info.
Method: Specifies the type of method requested, GET, POST, PUT, delete, etc.
Consumes: Specifies the type of submission to process the request (Content-type), such as Application/json, text/html;
Produces: Specifies the type of content returned, only if the type in the request header (Accept) contains the specified type to return
Params: Specifies that certain parameter values must be included in the request for the method to process
Headers: Specifies that certain header values must be included in the request in order for the method to process requests

Among them, consumes, produces use Content-typ information to filter information, headers can use Content-type to filter and judge.

3. Using the example

3.1 Headers
[HTML] view plain copy @RequestMapping (value = "/test", method = Requestmethod.get, headers= "referer=http://www . ifeng.com/") public void Testheaders (@PathVariable string ownerid, @PathVariable string petid) {//Impleme Ntation omitted} Here the headers inside can match all the information that can appear in the header, not limited to the referer information.

Example 2
[HTML] view plain copy @RequestMapping (value = "/response/contenttype", headers = "Accept=application/json") public void Response2 (HttpServletResponse response) throws IOException {//The media type that represents the content area data for the response is in JSON format and is encoded as UTF-8 (the client should         Decoding with Utf-8) Response.setcontenttype ("Application/json;charset=utf-8");         Write the response body content String Jsondata = "{\" username\ ": \" zhang\ ", \" password\ ": \" 123\ "}";     Response.getwriter (). write (Jsondata); The server produces JSON data based on the request header "Accept=application/json".

When you have the following accept header, you will follow the following rules to apply:
①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
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.
Requests Part

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.