How to config spring boot application to support both UTF-8 and GBK encode?
Adding the CharacterEncodingFilter bean can solve the problem ,seeing form https://github.com/spring-projects/spring-boot/issues/1182
@Bean
CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
filter.setForceEncoding(true);
return filter;
}
NikoTung
Updated on June 04, 2022Comments
-
NikoTung over 1 year
I am using spring boot in my project and I run some encoding issue.
In the project, there is a controller(below) which accept request with a content type header ,"application/x-www-form-urlencoded;charset=GBK".
@RequestMapping(value = "/notify",headers ={"Content-Type=application/x-www-form-urlencoded;charset=GBK"} , method = RequestMethod.POST, produces = "application/x-www-form-urlencoded; charset=GBK") public ResponseEntity<String> notify(@RequestParam(name = "p") String plain, @RequestParam("s") String signature), HttpServletRequest request){}
When the third party invoke this api ,they encode the request body by GBK.Once the body contain Chinese charsets,the parameter I got is wrong,which is not human readable, something like this "result������Ʒ".
Because the client send the request body with GBK encode,but the spring boot decode the request body with UTF-8 which is the default charset encode of spring boot.
The project is available different third-parties,most of them are using UTF-8,so I can not change the project encode to GBK by config the yml file with the following:
spring: http: encoding: charset: GBK enabled: true
So my first thought is to reverse the wrong string I got.But I fail with the following test.
String para = "p=result中文的&s=ad98adj"; byte[] bytes = para.getBytes("GBK"); ByteChunk byteChunk = new ByteChunk(); byteChunk.setBytes(bytes , 0 , bytes.length); byteChunk.setCharset(Charset.forName("utf-8")); String receive = byteChunk.toString();//this is the wrong string //reverse byteChunk.reset(); bytes = receive.getBytes("GBK"); byteChunk.setBytes(bytes , 0 ,bytes.length); byteChunk.setCharset(Charset.forName("GBK")); receive = byteChunk.toString(); //still the wrong string
So How can I use a single spring boot application to support both GBK and UTF-8 encode request.
-
rougou almost 5 yearsThis answer also worked for me, although I don't pretend to understand why. Setting UTF-8 and force in the application.properties has the opposite effect (which I would expect, since you are trying to force UTF-8 when it is not). There must be some other magic going on, but I'm satisfied with my answer for now.
-
Dmitry Kaltovich over 3 yearsYour suggestion is not solving problem :(
-
rougou over 3 years@DmitryKaltovich Which problem? Which encoding is incorrect? Spring looks at the request encoding (request.getCharacterEncoding()), so if the wrong encoding is specified, it won't work. In that case you can provide your own filter and call request.setCharacterEncoding() to override the encoding as you please.
-
Eldhose about 3 yearsThis worked for me, i was having issues in accepting different charsets in the springboot requests. Everything was getting defaulted to UTF-8. Adding this flag fixed it.