Http请求中Accept、Content-Type讲解以及在S

2018-11-26  本文已影响0人  vailter

title: Http请求中Accept、Content-Type讲解以及在Spring MVC中的应用
tags: http,spring mvc
grammar_cjkRuby: true


1.名词解释

2.HTTP协议传输的媒体类型及如何表示媒体类型

MediaType,即是Internet Media Type,互联网媒体类型;也叫做MIME类型,在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息。

类型格式:type/subtype(;parameter)? type
主类型,任意的字符串,如text,如果是号代表所有;
subtype 子类型,任意的字符串,如html,如果是
号代表所有;
parameter 可选,一些参数,如Accept请求头的q参数, Content-Type的 charset参数。
例如:Content-Type: text/html;charset:utf-8;

常见的MediaType:


Conteny-Type:内容类型,即请求/响应的内容区数据的媒体类型
Accept:用来指定什么媒体类型的响应是可接受的,即告诉服务器我需要什么媒体类型的数据,此时服务器应该根据Accept请求头生产指定媒体类型的数据。
问题:

@RequestMapping(value = "/response/ContentType", headers = "Accept=application/json")
public void response2(HttpServletResponse response) throws IOException {
    //表示响应的内容区数据的媒体类型为json格式,且编码为utf-8(客户端应该以utf-8解码)
    response.setContentType("application/json;charset=utf-8");
    //写出响应体内容
    String jsonData = "{\"username\":\"zhang\", \"password\":\"123\"}";
    response.getWriter().write(jsonData);
}
@Controller    
@RequestMapping(value = "/users", method = RequestMethod.POST, consumes="application/json", produces="application/json")    
@ResponseBody  
public List<User> addUser(@RequestBody User userl) {        
    // implementation omitted    
    return List<User> users;  
}

上面两个例子都表示了request请求中Accept头中包含了"application/json"的请求,同时暗示了返回的内容类型为application/json:

其中request Content-Type为“application/json”类型的请求.

当你有如下Accept头,将遵守如下规则进行应用:

即匹配规则为:最明确的优先匹配。

3.Spring MVC中关于关于Content-Type类型信息的使用

总结:

4.案例

GET
    url//请求的url
        http://localhost:9080/api/thirdparty/policy?id=1
    header//请求时header中携带的参数
        Content-Type:application/json; charset=utf-8 //是否定义没啥区别
    queryString//请求时携带的参数
        name=abc    //无法定义成json格式,必须是key=value格式
    springmvc//后台接收的方式
        String name
        @RequestParam String name //有无@RequestParam都行
        //无法使用@RequestBody,因为get没有body

POST
    url
        http://localhost:9080/api/thirdparty/policy?id=1
    header
        Content-Type未定义 //默认值Content-Type:application/x-www-form-urlencoded; charset=UTF-8
    body
        name=abc    //若body中的是json格式数据如:"{'name':'abc'}",则后台无法获取到参数name,从而无法访问接口
    springmvc
        @RequestBody String name 
            name="abc"
        String id
            id="1"
 
POST
    url
        http://localhost:9080/api/thirdparty/policy?id=1
    header
        Content-Type:application/json; charset=utf-8
    body
        {'name':'abc'}
    springmvc
        @RequestBody String body //@RequestBody是获取request的整个body体的数据,并且只能获取body中的数据,如果body中没有json数据,请求则报错Required request body is missing:
            body="{'name':'abc'}"
        String id
            id="1"
 
POST
    url
        http://localhost:9080/api/thirdparty/policy?id=1
    header
        Content-Type未定义 //默认值Content-Type:application/x-www-form-urlencoded; charset=UTF-8
    body
        {'name':'abc'}
    springmvc
        @RequestBody String body //这时的@RequestBody是获取queryString的参数+body参数URL编码后的值,因为有{}字符
            body="id=1&%7B%27name%27%3A%27abc%27%7D="
        @RequestParam String id
            id="1"
 
POST
    url
        http://localhost:9080/api/thirdparty/policy?id=1
    header
        Content-Type:application/x-www-form-urlencoded; charset=UTF-8
    body
        name=abc
    springmvc
        @RequestBody String body //这时的@RequestBody是获取queryString的参数+body参数URL编码后的值
            body="id=1&name=abc"
        @RequestParam String id
            id="1"
DELETE
    url
        http://localhost:9080/api/thirdparty/policy?id=1
    header
        Content-Type未定义 //默认值Content-Type:application/x-www-form-urlencoded; charset=UTF-8
    body
        name=abc
    springmvc
        @RequestBody String body //这时的@RequestBody是获取整个body数据,注意和post不同哦
            body="name=abc"
        @RequestParam String id
            id="1"
 
 
DELETE
    url
        http://localhost:9080/api/thirdparty/policy?id=1
    header
        Content-Type:application/json; charset=utf-8
    body
        {'name':'abc'}
    springmvc
        @RequestBody String body //这时的@RequestBody是获取整个body数据,同样请求时未有body数据就报错
            body="{'name':'abc'}"
        @RequestParam String id
            id="1"
PUT
    url
        http://localhost:9080/api/thirdparty/policy?id=1
    header
        Content-Type未定义 //默认值Content-Type:application/x-www-form-urlencoded; charset=UTF-8
    body
        {'name':'abc'}
    springmvc
        @RequestBody String body //这时的@RequestBody是获取整个body数据,同样请求时未有body数据就报错
            body="{'name':'abc'}"
        @RequestParam String id
            id="1"
PUT
    url
        http://localhost:9080/api/thirdparty/policy?id=1
    header
        Content-Type:application/json; charset=utf-8
    body
        {'name':'abc'}
    springmvc
        @RequestBody String body //这时的@RequestBody是获取整个body数据,同样请求时未有body数据就报错
            body="{'name':'abc'}"
        @RequestParam String id
            id="1"
PATCH
    url
        http://localhost:9080/api/thirdparty/policy?id=1
    header
        Content-Type:application/json; charset=utf-8
    body
        {'name':'abc'}
    springmvc
        @RequestBody String body //这时的@RequestBody是获取整个body数据,同样请求时未有body数据就报错
            body="{'name':'abc'}"
        @RequestParam String id
            id="1"
 
PATCH
    url
        http://localhost:9080/api/thirdparty/policy?id=1
    header
        Content-Type未定义 //默认值Content-Type:application/x-www-form-urlencoded; charset=UTF-8
    body
        {'name':'abc'}
    springmvc
        @RequestBody String body //这时的@RequestBody是获取整个body数据,同样请求时未有body数据就报错
            body="{'name':'abc'}"
        @RequestParam String id
            id="1"

^[footnote text]
参考资料:
https://blog.csdn.net/u014044812/article/details/78455053

上一篇 下一篇

猜你喜欢

热点阅读