解決SpringBoot請求返回字符串中文亂碼的問題
問題
當Controller的接口返回字符串,在SwaggerUI中測試時,發(fā)現(xiàn)返回都是問號,比如”?????id 100 ???????“,這是由于字符編碼問題導致
例如:
ResponseEntity.status(HttpStatus.NOT_FOUND) .body(String.format("未找到相應id %d 的記錄", id));
網上解決方案
現(xiàn)有的兩種解決方案:
- 第一種,針對單獨接口,在RequestMapping里設置 produces = {"text/plain;charset=UTF-8"}
- 第二種,統(tǒng)一在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“方法后,會導致一些其他問題
比如,統(tǒng)一處理異常的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 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
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
關于SpringGateway調用服務 接受不到參數(shù)問題
這篇文章主要介紹了關于SpringGateway調用服務接受不到參數(shù)問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12Mybatis?Web中的數(shù)據(jù)庫操作方法舉例詳解
Mybatis是一款優(yōu)秀的持久化框架,用于簡化JDBC的開發(fā),下面這篇文章主要給大家介紹了關于Mybatis?Web中數(shù)據(jù)庫操作方法的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-09-09使用Java實現(xiàn)在Excel中添加動態(tài)數(shù)組公式
動態(tài)數(shù)組公式是?Excel?引入的一項重要功能,它允許用戶從單個單元格中的公式返回多個結果值,并將這些值自動填充到與公式單元格相鄰的單元格中,本文主要介紹了如何使用Java實現(xiàn)在Excel中添加動態(tài)數(shù)組公式,x需要的可以參考下2023-12-12