Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Spring framework Request requests content-Type settings and configuration

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

1. Content-Type

MediaType, that is, Internet MediaType, Internet media type; also known as MIME type, in the Http protocol header, Content-Type is used to represent the media type information in a specific request.

Common media formats are as follows:

Text/html: HTML format

Text/plain: plain text format

Text/xml: XML format

P_w_picpath/gif: gif picture format

P_w_picpath/jpeg: jpg picture format

P_w_picpath/png:png picture format

Type of media format that begins with application:

Application/xhtml+xml: XHTML format

Application/xml: XML data format

Application/atom+xml: Atom XML 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:

The default encType,form form data in is encoded and sent to the server in key/value format (the form's default format for submitting data)

Another common media format is used when uploading files:

Multipart/form-data: this format is required when you need to upload files in the form

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.

@ Target ({ElementType.METHOD, ElementType.TYPE})

@ Retention (RetentionPolicy.RUNTIME)

@ Documented

@ Mapping

Public @ interface RequestMapping {

String [] value () default {}

RequestMethod [] method () default {}

String [] params () default {}

String [] headers () default {}

String [] consumes () default {}

String [] produces () default {}

}

The meaning of each parameter:

Value: specify the actual address of the request, such as / action/info and so on.

Method: specify the method type of the request, such as GET, POST, PUT, DELETE, etc.

Consumes: specifies the submitted content type (Content-Type) to process the request, such as application/json, text/html

Produces: specifies the content type to be returned, only if the (Accept) type in the request request header contains the specified type

Params: specifies that some parameter values must be included in the request for this method to handle the

Headers: specifies that some specified header value must be included in the request for this method to process the request

Among them, consumes and produces use content-typ information to filter information; content-type can be used in headers to filter and judge.

3. Use the example

3.1 headers

@ RequestMapping (value = "/ test", method = RequestMethod.GET, headers= "Referer= http://www.ifeng.com/")"

Public void testHeaders (@ PathVariable String ownerId, @ PathVariable String petId) {

/ / implementation omitted

}

The Headers here can match all the information that can appear in the Header, not limited to Referer information.

3.2 example of params

@ RequestMapping (value = "/ test/ {userId}", method = RequestMethod.GET, params= "myParam=myValue")

Public void findUser (@ PathVariable String userId) {

/ / implementation omitted

}

Processing requests only contains requests named "myParam" with a value of "myValue", which acts as a filter.

3.3 consumes/produces

@ Controller

@ RequestMapping (value = "/ users", method = RequestMethod.POST, consumes= "application/json", produces= "application/json")

@ ResponseBody

Public List addUser (@ RequestBody User userl) {

/ / implementation omitted

Return List users

}

Method handles only requests of type "application/json" with request Content-Type. The produces identity = = > processes requests with "application/json" in the accept header of the request request, implying that the returned content type is application/json

4. Application scenario

Requests submitted by GET or POST:

Content-type: 1. Application/x-www-form-urlencoded:@RequestBody is not required.

2. Mutipart/form-data:@RequestBody cannot handle this format

3. Other formats, such as application/json,application/xml, must be handled with @ RequestBody

The @ ResquestBody annotation enables the http message converter to parse the request body according to content-type

Request submitted in PUT mode:

The above 1 and 3 scenarios must be handled with @ RequestBody, and 2 scenarios are not supported.

Example:

Such as: $.ajax ({

Url: "/ login"

Type: "post"

ContentType: "application/json;charset=UTF-8"

Data:JSON.stringify ({"name": "test", "pass": "123456"})

});

Backend receiving code example:

@ RequestMapping (value= "/ login")

@ ResponseBody

Public ModelAndView loadForm (@ RequestBody Login login) {

/ / implementation omitted

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report