Http的那些事: Content-Type
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的更多相关文章
- Jsoup问题---获取http协议请求失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.
Jsoup问题---获取http协议请求失败 1.问题:用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不 ...
- Jsoup获取部分页面数据失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.
用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不符合要求. 请求代码如下: private static ...
- SharePoint自动化系列——Add content type to list.
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 将创建好的content type(若是跨web application需要事先publish c ...
- SharePoint自动化系列——Content Type相关timer jobs一键执行
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 背景: 在SharePoint Central Administration->Monito ...
- 转载 SharePoint【Site Definition 系列】– 创建Content Type
转载原地址: http://www.cnblogs.com/wsdj-ITtech/archive/2012/09/01/2470274.html Sharepoint本身就是一个丰富的大容器,里面 ...
- 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 ...
- 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 ...
- springboot 报错 Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
开始 controller 方法写的是 @RequestMapping( value = "/add", method = RequestMethod.POST ) public ...
- .NET获取文件的MIME类型(Content Type)
第一种:这种获取MIME类型(Content Type)的方法需要在.NET 4.5之后才能够支持,但是非常简单. 优点:方便快捷 缺点:只能在.NET 4.5之后使用 public FileResu ...
- 万能的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 ...
随机推荐
- hadoop Non DFS Used是什么
首先我们先来了解一下Non DFS User是什么? Non DFS User的意思是:非hadoop文件系统所使用的空间,比如说本身的linux系统使用的,或者存放的其它文件 它的计算公式: n ...
- oh_my_zsh
oh_my_zsh zsh默认已经安装 cat /etc/shells 查看安装没有 (https://xiaozhou.net/learn-the-command-line-iterm ...
- .NET并行计算和并发4-Thread-Relative Static Fields and Data Slots
Thread Local Storage: Thread-Relative Static Fields and Data Slots 文章摘自msdn library官方文档 可以使用托管线程本地存储 ...
- CreateThread给线程函数传递的参数
HANDLE WINAPI CreateThread ( __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, // 指向SECURITY_ATTR ...
- 使用CSMA/CD协议一个计算题
题干: 首先计算一下A这个以太网所容许的最短的帧它的发送帧的长度时间为: (8(前同步码为8)+64(最短帧长))*8(单位转换b到B)=576比特 有关于单位转换: B是Byte的缩写,B就是Byt ...
- ieee trans pami latex模板
https://www.computer.org/cms/Computer.org/transactions/templates/ https://www.computer.org/web/tpami ...
- C与指针练习题4.14.1
//C与指针练习题4.14.1 //ai+1=(ai+n/ai)/2公式逼近,当ai+1=ai时,取得n的平方根 #include<stdio.h> float sq_root(float ...
- Linux系统-禁ping
1) Add the following line to your /etc/sysctl.conf net.ipv4.icmp_echo_ignore_all=1 Then : sysctl -p ...
- es6学习日记2
1.字符串扩展 字符串的遍历器接口 for (let codePoint of 'foo') { console.log(codePoint) } // "f" // " ...
- Python全栈之路----函数进阶----名称空间
又名name space,顾名思义就是存放名字的地方,存什么名字呢?举例说明,若变量x=1,1存放于内存中,那名字x存放在哪里呢?名称空间正是存放名字x与1绑定关系的=地方 名称空间共3种,分别如下 ...