最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決SpringBoot請求返回字符串中文亂碼的問題

 更新時間:2024年05月31日 15:55:41   作者:古辛  
這篇文章主要介紹了解決SpringBoot請求返回字符串中文亂碼的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

問題

當Controller的接口返回字符串,在SwaggerUI中測試時,發(fā)現(xiàn)返回都是問號,比如”?????id 100 ???????“,這是由于字符編碼問題導(dǎo)致

例如:

ResponseEntity.status(HttpStatus.NOT_FOUND)
  .body(String.format("未找到相應(yīng)id %d 的記錄", id));

網(wǎng)上解決方案

現(xiàn)有的兩種解決方案:

  • 第一種,針對單獨接口,在RequestMapping里設(shè)置 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“導(dǎo)致,是通過重寫”configureMessageConverters“方法來設(shè)置UTF-8編碼來解決。

也就是第二種,坑了我,也許是我使用不當?

新解決方案

通過研究源碼,找到了新的解決思路:

因為通過重寫”configureMessageConverters“方法后,會導(dǎo)致一些其他問題

比如,統(tǒng)一處理異常的ExceptionAdviceHandler不工作,還導(dǎo)致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()));

并且調(diào)用下載接口時,會報406錯誤和異常”No converter for [class org.springframework.core.io.UrlResource]”,意思是不支持 “application/octet-stream“的轉(zhuǎn)換,見鬼了,通過測試,禁用掉WebMvcConfigurer的重寫,下載功能就ok了,但是會重新有編碼問題。

最終通過研究源碼,找到了根源,這是由于設(shè)置了自己的converter導(dǎo)致默認的其他converters不會再被初始化添加導(dǎo)致,參見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;
    }

所以基于這個代碼,我們則應(yīng)該重寫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對象,無需處理,且已經(jīng)提供了默認的UTF-8編碼,因為當默認沒有設(shè)置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

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

黑龙江省| 荃湾区| 城固县| 胶南市| 兴化市| 宁陵县| 富顺县| 安庆市| 洛宁县| 平乡县| 昌图县| 固原市| 手游| 新巴尔虎右旗| 三原县| 怀宁县| 措勤县| 泽州县| 淅川县| 色达县| 明星| 昭平县| 留坝县| 佛学| 和政县| 阿图什市| 林西县| 土默特右旗| 大埔县| 涞源县| 贵港市| 霍城县| 南木林县| 名山县| 水富县| 青龙| 萝北县| 体育| 榆中县| 钟祥市| 天气|