Content-Type 无疑是http中一个非常重要的属性了, request 中可以存在, 也可以不存在( request的Content-Type 默认是 */*, 实际上呢, 如果不存在Content-Type请求头, 那么 就是text.. 待确定 ), response也是这样. 如果是普通text/ css/ img, 响应头Content-Type 好像是找不到的了,  如果是json 返回, 那么 Content-Type 肯定是存在的 . 这, 非常的灵活...

@PostMapping("add")
@ApiOperation(value = "新增")
public String add(@RequestBody String groupName) {
return "hi" + groupName;
}
对应url : http://10.10.10.76:5555/mq-service-lk/test

参数应该怎么传呢? @RequestBody 表明了 Content-Type 应该是 application/json , 即
Content-Type: application/json

对于@RequestBody, Content-Type必须是 application/json 否则后端返回 415:
{
"timestamp": 1531815175323,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'text/plain;charset=UTF-8' not supported",
"path": "/test/bbb"
}

如果想使用 @RequestBody, 但是只有一个参数, 而且是 String, 那么只有把参数进行封装了(这样做确实非常别扭不情愿), 或者使用Map 参见: https://www.oschina.net/question/227902_162591

但是呢, 如果我们使用 postman, 我们发现:
1 不设置 Content-Type, body 填: { "groupaName": "aaaasdfsadf"} 完全是没问题的.. 也就是说, springmvc 的@RequestBody 并没有强制作用, 但是, 后端接收到的数据, 并不是json, 而是 一个字符串..
2 设置Content-Type: application/json, body 填: { "groupaName": "aaaasdfsadf"}, 出现400, 前端收到返回:

{
"timestamp": "2018-07-17 12:45:54",
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "JSON parse error: Can not deserialize instance of java.lang.String out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@3e291882; line: 1, column: 1]",
"path": "/test"
}

后端出现:
2018-07-17 11:14:59.233 WARN 18384 --- [nio-8083-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize instance of java.lang.String out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@7744171e; line: 1, column: 1]
(注意, 这里是 WARN, 不仔细看是发现不了的..)

为什么出现这样的情况呢? 非常奇怪了, 一样的代码, 在别的项目中没有重现,

@RestController
@RequestMapping("/test")
public class DemoController {

@PostMapping(value = "aaa")
public String add(@RequestBody String groupName) {
System.out.println("groupName = " + groupName);
return "hi, " + groupName;
}
...

参考了 https://blog.csdn.net/fzz1022/article/details/78649020, 好像是... jar 问题?

方法改成这样:
add(@Valid String groupName)
发现,

不管Content-Type 是什么, groupName 的值一直都是 null, 就是说, 根本传值不了..

方法改成这样:
add(String groupName), 再尝试:

post请求自动变成了 get 请求一样: http://10.10.10.76:5555/mq-service-lk/test?groupName=asdff 这时候PostMapping 跟 GetMapping 完全一样.. swagger 似乎也有坑.. 有时候需要刷新, 有时候有不需要..

注意, 此时, Content-Type: application/x-www-form-urlencoded 必须这样才可以!!!!!!!!! 其他 都不行, 而且 body 应该是这样的:
groupName=g1

坑爹了, 为啥我传递一个json 参数就这么难,, 我想做的只是, 传递这样格式的数据啊:
type 为 Content-Type: application/json, body 为:

{"groupaName": "g1"}
{ groupaName: "g1"}

groupaName 是否有引号, 好像不用紧的...

后面终于明白, add(@RequestBody String groupName) , 因为 groupName 类型是 String, 是无论如何无法 接收json 的,,, 必须把 groupName 进行封装, 比如封装到一个对象里面, 而后提供getter/setter.. 或者使用Map.

PostMapping 方法又是不允许 get 方式请求的..

非常蛋疼了, postman 的body 格式, 从 Content-Type: application/x-www-form-urlencoded 切换 到 Content-Type: application/json 的时候, 有个bug, 坑爹, Content-Type 竟然没有改变..

问题是, 为啥body 有时候要求是这样:
groupName=g1
有时候是:
{ groupaName: "g1"} 或 {"groupaName": "g1"}

而有时候是
g1 或"g1"

因为springmvc 可以对 Content-Type 设置一定的要求, 而且, 不同Content-Type 要求的body 也是不一样的..

改成这样:
add(@Valid String groupName) 发现 @Valid 有奇怪的作用, 导致完全接受不到 参数了. . 不管请求头的 Content-Type 怎么设置, groupName 都是null, 底层原因待查

总结:

1 对于 @RequestBody, body 是不能少的, 也就是说, 如果有@RequestBody 那么请求方法不能是get, 因为get 会忽略 已经设置的body, 不传送..
2 springmvc 可以对 Content-Type 设置一定的要求, 而且, 不同Content-Type 要求的body 也是不一样的
3 如果@RequestBody, GetMapping 同时添加到一个方法上, 那么会出现:
{"timestamp":"2018-07-17 13:15:14","status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Required request body is missing: public String com.xx.add(java.lang.String)","path":"/test"}
4 post 方法既传递url query string, 也传递request body, 如果同时存在, 那么会组装成一个数组, 或者逗号分隔的字符串..

Http的那些事: Content-Type的更多相关文章

  1. Jsoup问题---获取http协议请求失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.

    Jsoup问题---获取http协议请求失败 1.问题:用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不 ...

  2. Jsoup获取部分页面数据失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.

    用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不符合要求. 请求代码如下: private static ...

  3. SharePoint自动化系列——Add content type to list.

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 将创建好的content type(若是跨web application需要事先publish c ...

  4. SharePoint自动化系列——Content Type相关timer jobs一键执行

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 背景: 在SharePoint Central Administration->Monito ...

  5. 转载 SharePoint【Site Definition 系列】– 创建Content Type

    转载原地址:  http://www.cnblogs.com/wsdj-ITtech/archive/2012/09/01/2470274.html Sharepoint本身就是一个丰富的大容器,里面 ...

  6. the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header

    the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header ...

  7. Springs Element 'beans' cannot have character [children], because the type's content type is element-only

    Springs Element 'beans' cannot have character [children], because the type's content type is element ...

  8. springboot 报错 Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

    开始 controller 方法写的是 @RequestMapping( value = "/add", method = RequestMethod.POST ) public ...

  9. .NET获取文件的MIME类型(Content Type)

    第一种:这种获取MIME类型(Content Type)的方法需要在.NET 4.5之后才能够支持,但是非常简单. 优点:方便快捷 缺点:只能在.NET 4.5之后使用 public FileResu ...

  10. 万能的ctrl+shift+F(Element 'beans' cannot have character [children], because the type's content type is element-only.错误)

    今天在spring-servlet.xml文件中出现了一个莫名其妙的错误:Element 'beans' cannot have character [children], because the t ...

随机推荐

  1. intelij idea设置成eclipse快捷键

    1.导入jar包文件: https://pan.baidu.com/s/1QSd_CY5X_dUUw74evbckXg  密码: 23rq 2.idea -->settting ---> ...

  2. ubuntu svn服务本地搭建使用

    安装 sudo apt-get install subversion 创建一个仓库 svnadmin create mysvn 编辑配置文件  /home/exayong/mysvn就是上面创建的目录 ...

  3. Java学习笔记42(数据库连接池 druid连接池)

    druid连接池: 是阿里的连接池,druid的稳定性及效率都很高,目前用的比较广,所以建议开发过程中尽量用druid连接池(支持国产最重要) druid连接池也需要配置文件,配置文件必须是prope ...

  4. ADO.NET 的五个对象

    首先来一张关系图,了解大概关系. 知道了整个大关系之后,我们在具体看一下他们五个的分工: 1. SqlConnection 在访问数据时,我们首先必须要建立数据库的物理连接.· 2.SqlComman ...

  5. 1.oracle之表管理sql

    /*数据类型1. number(M,N)   整数位和小数位最多是M,其中小数位为N位2. char(M):定长字符串,长度为M,如果插入数据时长度小于M,则在末尾补上空格3. varchar2(M) ...

  6. 如何在Linux下添加函数库

    如何为Linux增加库一. 静态库在Linux下的静态库是以.a为后缀的文件.1. 建静态库h1.c 源文件#include<stdio.h>void hello1(){printf(“t ...

  7. 构建gulp项目

    express是node.js中的构建工具,如果需要使用express构建,首先需要安装express. 构建一个项目: |-- app| |-- css| |-- js| | `-- class| ...

  8. kettle在本地执行向远程hdfs执行转换错误"Couldn't open file hdfs"

    kettle在本地执行向远程hdfs执行转换时,会出现以下错误: ToHDFS.0 - ERROR (version 7.1.0.0-12, build 1 from 2017-05-16 17.18 ...

  9. 2java判断素数

    package com.test; import java.math.*;import java.util.Scanner; public class test222 { /** * @param a ...

  10. 编写 python 小程序,将LOL官网的皮肤保存下来,上传百度云,记录那些强撸灰飞烟灭的日子

    to 撸的血泪史:大学四年几乎都在宿舍打撸,So,把官网的皮肤都保存下来,存到百度云,就当一种纪念 编辑器:pycharm 用到的包:urllib.request, requests, json, r ...