springboot响应消息(http)的编码设置

Posted yoyotl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot响应消息(http)的编码设置相关的知识,希望对你有一定的参考价值。

一、方式一

在单个REST接口上设置

    @ResponseBody
    @RequestMapping(value = "sys/getTree1",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
    public String getTree1()
        return "success";
    

二、方式二

全局配置(推荐)

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport 

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) 
        converters.add(responseBodyConverter());
        super.configureMessageConverters(converters);
    
    @Bean
    public HttpMessageConverter responseBodyConverter() 
        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    

 

以上是关于springboot响应消息(http)的编码设置的主要内容,如果未能解决你的问题,请参考以下文章