SpringBoot中文乱码排查

  |   0 评论   |   79 浏览

现象

返回的头为:

Content-Type: text/html;charset=ISO-8859-1

解决方法:

produces

增加 produces如下所示

    @GetMapping(value = "/get/something", produces = "text/json;charset=UTF-8")
    public String getSomething() {
    }

字符编码配置类

来自springboot全局字符编码设置(解决乱码问题),本博未验证。


/**
 * 中文乱码解决
 */
@Configuration
public class CharsetConfig extends WebMvcConfigurerAdapter {
    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(
                Charset.forName("UTF-8"));
        return converter;
    }
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(responseBodyConverter());
    }
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }

参考