java 如何配置 spring boot 应用程序以支持 UTF-8 和 GBK 编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39939555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:47:17  来源:igfitidea点击:

How to config spring boot application to support both UTF-8 and GBK encode?

javaencodingutf-8gbk

提问by NikoTung

I am using spring boot in my project and I run some encoding issue.

我在我的项目中使用 spring boot 并且我运行了一些编码问题。

In the project, there is a controller(below) which accept request with a content type header ,"application/x-www-form-urlencoded;charset=GBK".

在项目中,有一个控制器(下面)接受内容类型标题为“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???????".

当第三方调用这个api时,他们用GBK对请求体进行编码。一旦正文包含中文字符集,我得到的参数是错误的,不是人类可读的,类似于“结果???????”。

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.

因为客户端发送的请求体使用 GBK 编码,但是 Spring Boot 使用 UTF-8 解码请求体,这是 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:

该项目可用于不同的第三方,其中大多数使用 UTF-8,因此我无法通过使用以下配置 yml 文件将项目编码更改为 GBK:

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.

那么如何使用单个 spring 启动应用程序来支持 GBK 和 UTF-8 编码请求。

回答by NikoTung

Adding the CharacterEncodingFilter bean can solve the problem ,seeing form https://github.com/spring-projects/spring-boot/issues/1182

添加CharacterEncodingFilter bean可以解决问题,见表格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;
}

回答by rougou

I had a similar problem and found that Spring Boot has "forceEncoding" enabled by default. This causes the request charset to be overridden and set to UTF-8 every time in their filter.

我遇到了类似的问题,发现 Spring Boot 默认启用了“forceEncoding”。这会导致请求字符集被覆盖并每次在它们的filter 中设置为 UTF-8 。

See Appendix A. Common application properties

请参阅附录 A. 常见应用程序属性

The key part is:

关键部分是:

Defaults to true when "force" has not been specified.

当未指定“force”时,默认为 true。

So setting either

所以设置要么

spring.http.encoding.force=false

or

或者

spring.http.encoding.force-request=false

Should solve your problem, as long as the request has the correct headers.

只要请求具有正确的标头,就应该可以解决您的问题。