Windows下RequestBody里中文格式JSON解析报错
springboot内置tomcat启动服务出现异常提示中文乱码,保存数据json解析异常等问题,折腾了半天,原来用cmd窗口直接启动跟放bat里执行效果是不一样的:
走过的弯路
1、配置1无效
# Tomcat
server:
port: 8089
tomcat:
uri-encoding: UTF-8
2、配置2无效
spring:
http:
encoding:
force: true
charset: UTF-8
enabled: true
3、java代码调整produces = {“application/json; charset=utf-8”}) 无效
发现java代码里没有写produces = {“application/json; charset=utf-8”})
@PostMapping(value = "/add", produces = {"application/json; charset=utf-8"})
4、直接cmd执行 java -jar命令无效
异常提示中文乱码
提交数据json解析异常
400 BAD_REQUEST "Failed to read HTTP message"; nested exception is org.springframework.core.codec.DecodingException: JSON decoding error: Invalid UTF-8 start byte 0xb6; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 start byte 0xb6 at [Source: (io.netty.buffer.ByteBufInputStream); line: 1, column: 54] (through reference chain: com.evn.gateway.entity.GatewayRoute["serviceName"])
解决方式 用bat执行,运行正常
1、通过bat启动程序
2、启动命令添加“-Dfile.encoding=UTF-8”
启动命令(java -Dfile.encoding=UTF-8 -jar -Xms128m -Xmx256m XXX.jar)
docker启动(docker run -e JAVA_OPTS=‘-server -Xms128m -Xmx128m -Dfile.encoding=UTF-8’ -d --name name -p port:portimage:1.0)
在双击执行bat启动生效 ,提示正常了,提交数据也能成功。