springboot @RestController 返回中文乱码

1、在具体的方法上指定返回编码

@RequestMapping(value = "index", produces = "text/html;charset=utf-8")

 

2  、WebMvcConfigurer统一配置

    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        return new StringHttpMessageConverter(Charset.forName("UTF-8"));
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(responseBodyConverter());
    }

 

posted @ 2020-12-02 00:10  落孤秋叶  阅读(2315)  评论(3编辑  收藏  举报