解决SpringBoot请求返回字符串中文乱码的问题

这篇文章主要介绍了解决SpringBoot请求返回字符串中文乱码的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

问题
网上解决方案
新解决方案
JSON格式的编码探讨
总结

问题
当Controller的接口返回字符串,在SwaggerUI中测试时,发现返回都是问号,比如”?????id 100 ???????“,这是由于字符编码问题导致

例如:

ResponseEntity.status(HttpStatus.NOT_FOUND)
  .body(String.format("未找到相应id %d 的记录", id));

网上解决方案
现有的两种解决方案:

第一种,针对单独接口,在RequestMapping里设置 produces = {"text/plain;charset=UTF-8"}
第二种,统一在MVC配置类中,通过修改StringHttpMessageConverter默认配置,部分代码(PS,该代码从别处拷贝而来):

@Configuration
@EnableWebMvc
public class MyMvcConfig implements WebMvcConfigurer {
  
    @Bean
    public HttpMessageConverter<String> responseBodyStringConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        return converter;
    }
  
  
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters){
        converters.add(responseBodyStringConverter());
    }
}

是由于默认的编码是”StandardCharsets.ISO_8859_1“导致,是通过重写”configureMessageConverters“方法来设置UTF-8编码来解决。

也就是第二种,坑了我,也许是我使用不当?

新解决方案
通过研究源码,找到了新的解决思路:

因为通过重写”configureMessageConverters“方法后,会导致一些其他问题

比如,统一处理异常的ExceptionAdviceHandler不工作,还导致Controller接口不支持文件下载

比如:

//解决中文文件名的乱码问题
String utf8 = StandardCharsets.UTF_8.name();
try {
    downloadFileName = URLEncoder.encode(downloadFileName, utf8);
} catch (UnsupportedEncodingException e) {
    //
}
  
return ResponseEntity.ok()
        .contentType(MediaType.APPLICATION_OCTET_STREAM)
        .header(HttpHeaders.CONTENT_DISPOSITION,
                "attachment; filename* = " + utf8 + "''" + downloadFileName)
        .body(new UrlResource(downloadFile.toURI()));

并且调用下载接口时,会报406错误和异常”No converter for [class org.springframework.core.io.UrlResource]”,意思是不支持 “application/octet-stream“的转换,见鬼了,通过测试,禁用掉WebMvcConfigurer的重写,下载功能就ok了,但是会重新有编码问题。

最终通过研究源码,找到了根源,这是由于设置了自己的converter导致默认的其他converters不会再被初始化添加导致,参见WebMvcConfigurationSupport的代码:

protected final List<HttpMessageConverter<?>> getMessageConverters() {
    if (this.messageConverters == null) {
        this.messageConverters = new ArrayList();
        this.configureMessageConverters(this.messageConverters);
        if (this.messageConverters.isEmpty()) {
            this.addDefaultHttpMessageConverters(this.messageConverters);
        }
 
        this.extendMessageConverters(this.messageConverters);
    }
 
    return this.messageConverters;
}

所以基于这个代码,我们则应该重写extendMessageConverters方法来达到目的,最终的代码是:

@Bean
 public HttpMessageConverter<String> responseBodyStringConverter() {
     StringHttpMessageConverter converter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
     return converter;
 }
 
 @Override
 public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
     List<StringHttpMessageConverter> stringHttpMessageConverters = converters.stream()
             .filter(converter -> converter.getClass().equals(StringHttpMessageConverter.class))
             .map(converter -> (StringHttpMessageConverter) converter)
             .collect(Collectors.toList());
 
     if (stringHttpMessageConverters.isEmpty()) {
         converters.add(responseBodyStringConverter());
     } else {
         stringHttpMessageConverters.forEach(converter -> converter.setDefaultCharset(StandardCharsets.UTF_8));
     }
 }

JSON格式的编码探讨
这里仅处理接口直接返回字符串的问题,而对于处理JSON返回,这是因为JSON返回由MappingJackson2HttpMessageConverter来控制:

protected JsonEncoding getJsonEncoding(@Nullable MediaType contentType) {
    if (contentType != null && contentType.getCharset() != null) {
        Charset charset = contentType.getCharset();
        for (JsonEncoding encoding : JsonEncoding.values()) {
            if (charset.name().equals(encoding.getJavaName())) {
                return encoding;
            }
        }
    }
    return JsonEncoding.UTF8;
}

所以对于返回JSON对象,无需处理,且已经提供了默认的UTF-8编码,因为当默认没有设置MediaType的编码格式时,则会使用该默认的UTF-8编码。

并且MediaType中针对JSON的编码有如下解释:

/**
 * A String equivalent of {@link MediaType#APPLICATION_JSON_UTF8}.
 * @deprecated as of 5.2 in favor of {@link #APPLICATION_JSON_VALUE}
 * since major browsers like Chrome
 * <a href="https://bugs.chromium.org/p/chromium/issues/detail?id=438464" rel="external nofollow" >
 * now comply with the specification</a> and interpret correctly UTF-8 special
 * characters without requiring a {@code charset=UTF-8} parameter.
 */
@Deprecated
public static final String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";
PS:org.springframework.boot:spring-boot-starter-web:jar:2.2.1.RELEASE

总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持vb.net教程C#教程python教程SQL教程access 2010教程xin3721自学网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您好,关于您提到的问题,可能是由于编码问题导致的。您可以尝试在返回数据时指定正确的编码格式,或者在前端页面中设置正确的编码格式来解决乱码问题。如果您需要更具体的帮助,请提供更多细节信息,我会尽力帮助您解决问题。 ### 回答2: 当我们使用Spring Boot Model返回数据时,遇到中文乱码问题的原因通常是由于字符编码不一致所导致的。 为了解决这个问题,我们可以采取以下几种方法: 1. 指定字符编码: 可以在Spring Boot的配置文件(application.properties或application.yml)中设置`spring.http.encoding.charset`属性,将字符编码设置为UTF-8。如: ``` spring.http.encoding.charset=UTF-8 ``` 这样就会将返回的数据以UTF-8编码进行处理,避免中文乱码问题。 2. 通过设置Content-Type头信息: 在返回数据的Controller方法上,添加`produces`属性,将返回的数据类型设置为"text/html;charset=UTF-8"。 ```java @RequestMapping(value = "/example", produces = "text/html;charset=UTF-8") public String example() { // 返回数据的业务逻辑 } ``` 这样就会将返回的数据类型设置为UTF-8编码,确保中文正常显示。 3. 使用@ResponseBody注解: 在Controller方法上使用`@ResponseBody`注解,将返回的数据直接转换为JSON格式,确保字符编码一致。如: ```java @RequestMapping(value = "/example") @ResponseBody public String example() { // 返回数据的业务逻辑 } ``` 通过这种方式,我们可以确保返回的数据以UTF-8编码进行处理,避免中文乱码问题。 总结来说,解决Spring Boot Model返回的数据中文乱码问题,关键是要确保字符编码一致,一般情况下,设置字符编码为UTF-8即可避免中文乱码问题的出现。 ### 回答3: 当我们在使用 Spring Boot 时,有时会遇到返回的数据中文乱码问题。这通常是由于编码设置不正确引起的。 要解决这个问题,我们可以通过以下几种方式来设置正确的编码: 1. 在 application.properties 或 application.yml 文件中添加以下配置: ``` spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true spring.http.encoding.force=true ``` 这样会强制使用 UTF-8 编码进行数据传输,确保中文字符能够正确显示。 2. 在 Controller 的方法上添加 `produces = "text/plain;charset=UTF-8"` 注解,指定返回数据的编码格式为 UTF-8。例如: ```java @GetMapping(value = "/getData", produces = "text/plain;charset=UTF-8") public String getData() { // 返回数据 } ``` 这样可以确保返回的数据按照 UTF-8 编码进行传输。 3. 使用 ResponseEntity 对象返回数据,并设置正确的编码格式。例如: ```java @GetMapping("/getData") public ResponseEntity<String> getData() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); headers.set(HttpHeaders.CONTENT_ENCODING, "UTF-8"); // 构造返回数据 String data = "中文数据"; return new ResponseEntity<>(data, headers, HttpStatus.OK); } ``` 在这种方式下,我们可以通过设置 HttpHeaders 的 CONTENT_ENCODING 来指定返回数据的编码格式为 UTF-8。 通过以上方法的设置,应该能够解决返回的数据中文乱码问题。如果问题仍然存在,可能涉及到其他原因,例如数据库字符集设置不正确等,需要根据具体情况进行进一步排查和处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值