SpringBoot请求返回字符串中文乱码,修改默认StringHttpMessageConverter配置

Controller

@Api("MistraController")
@RestController
@RequestMapping(value = "/mistra")
public class MistraController {

    @ApiOperation("启动测试")
    @GetMapping(value = "/test")
    public String test() {
        return "☆☆  项目启动成功  ☆☆";
    }
}

返回String数据到浏览器时中文乱码。查看Response返回编码:
SpringBoot请求返回字符串中文乱码,修改默认StringHttpMessageConverter配置_第1张图片
格式跟浏览器格式不匹配,需要UTF-8。
查看StringHttpMessageConverter源码(截取部分):

public class StringHttpMessageConverter extends AbstractHttpMessageConverter {
	//万恶之源,默认就是ISO_8859_1
	public static final Charset DEFAULT_CHARSET = StandardCharsets.ISO_8859_1;
}

再看看人家HttpEncodingProperties默认的就是UTF-8(截取部分),这样多好:

@ConfigurationProperties(prefix = "spring.http.encoding")
public class HttpEncodingProperties {

	public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
}

解决方法一
在RequestMapping里设置 produces = { “application/json;charset=UTF-8” }:
SpringBoot请求返回字符串中文乱码,修改默认StringHttpMessageConverter配置_第2张图片
但是麻烦,每个controller都要写一次。


解决方法二
在MVC配置类中修改StringHttpMessageConverter默认配置:

package com.spring.boot.test.springMVC;

import com.spring.boot.test.interceptor.MyInterceptor;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * Author: RoronoaZoro丶WangRui
 * Time: 2018/6/21/021
 * Describe:
 */
@Configuration
@EnableWebMvc
@ComponentScan("com.spring.boot.test.springMVC")
public class MyMvcConfig implements WebMvcConfigurer {

    @Bean
    public MyInterceptor myInterceptor(){
        return new MyInterceptor();
    }

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/classes/views/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setViewClass(JstlView.class);
        return viewResolver;
    }

    /**
     * 配置静态资源访问
     * addResourceHandler 暴露的访问地址
     * addResourceLocations 文件存放位置
     * http://localhost:8080/static/1.jpg
     * @param resourceHandlerRegistry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry resourceHandlerRegistry) {
        resourceHandlerRegistry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    /**
     * 配置拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor());
    }


    @Bean
    public HttpMessageConverter responseBodyStringConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        return converter;
    }

    /**
     * 修改StringHttpMessageConverter默认配置
     * @param converters
     */
    @Override
    public void configureMessageConverters(List> converters){
        converters.add(responseBodyStringConverter());
    }
}

参考:SpringMVC-StringHttpMessageConverter简介
完。

你可能感兴趣的:(SpringMVC,SpringBoot)