突然发现RequestBody接受参数中文乱码了,本地没问题,服务器上总是报错,并且只有中文大于3个字符才报错,奇怪
Caused by: com.fasterxml.jackson.core.JsonParseException: Invalid UTF-8 start byte 0xa4
at [Source: (PushbackInputStream); line: 1, column: 24]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1851)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:707)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidInitial(UTF8StreamJsonParser.java:3601)
看了linux服务器的编码,尽然是GBK,然后springboot代码里面明明用的UTF-8还不会自动切换,无语了,只能在启动脚本加上
public static void main(String[] args) {
//解决linux服务器编码不是UTF-8会导致springboot @RequestBody获取参数乱码的问题
System.setProperty("file.encoding","UTF-8");
try {
Field charset = Charset.class.getDeclaredField("defaultCharset");
charset.setAccessible(true);
charset.set(null,null);
}catch(Exception e){
e.printStackTrace();
}
SpringApplication.run(MdpMgrServerApplication.class, args);
}
然后就可以了