java 报文长度_HTTP请求header之Content-Length

本文探讨了在HTTP请求中,Content-Length头配置过小导致JSON实体被截断的问题,介绍了登录验证中如何识别该问题,并给出了修正方法,建议避免使用Content-Length或增大其值以确保数据完整传递。
摘要由CSDN通过智能技术生成

首先说什么是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请求头,或者设置大一点即可

参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值