首先说什么是Content-Length,在http的协议中Content-Length头部告诉浏览器报文中实体数据的大小。
说到对Content-Length的关注源于一个项目报错信息
Could not read document: Unexpected end-of-input in VALUE_STRING
at [Source: java.io.PushbackInputStream@331dba5d; line: 4, column: 82]
at [Source: java.io.PushbackInputStream@331dba5d; line: 4, column: 14] (through reference chain: com.byron.service.user.model.UserLoginModel["loginType"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected end-of-input in VALUE_STRING
at [Source: java.io.PushbackInputStream@331dba5d; line: 4, column: 82]
at [Source: java.io.PushbackInputStream@331dba5d; line: 4, column: 14] (through reference chain: com.byron.service.user.model.UserLoginModel["loginType"])
Java代码中看loginType是枚举类型
@NotBlank(message = "账号不能为空")
private String username;
@NotBlank(message = "密码不能为空")
private String password;
private LoginType loginType =LoginType.pwd;
入参:
header
Content-Type:application/json
Content-Length:64
body
{
"username":"1322225678",
"password":"11",
"loginType":"pwd"
}
报错原因:
header中配置Content-Length参数,由于配置过小,所以JSON实体被自动截断了,导致最后参数无法匹配上.
修正:
最好不使用Content-Length请求头,或者设置大一点即可
参考: