Springboot 解决返回中文乱码问题

Posted 程序员超时空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot 解决返回中文乱码问题相关的知识,希望对你有一定的参考价值。

package com.caib.commons.handlerInterceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

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

/**
 * @Author 荡漾
 * @email liufei@cai100.net
 * @description: 配置拦截器
 * @create 2018-11-20 16:14
 */
@Component
public class MvcConfigurer extends WebMvcConfigurationSupport 
    //解决中文乱码问题
    @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 addInterceptors(InterceptorRegistry registry) 
        registry.addInterceptor(new SecurityHandlerInterceptor())
                //添加过滤url ** 代表所有
                .addPathPatterns("/**")
                //添加排除过滤url 
				.excludePathPatterns(
				        "/swagger-resources/**"
                        , "/webjars/**"
                        , "/v2/**"
                        , "/swagger-ui.html/**"
                        ,"/register"
                        ,"/login"
                        ,"/**/error");
;
        super.addInterceptors(registry);
    

    /**
     * 解决resources下面静态资源无法访问
     * @param registry
     */
   /* @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) 
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("classpath:/gen","classpath:application.yml","classpath:logback.xml");
        super.addResourceHandlers(registry);
    */

 /*   @Override
    public void addCorsMappings(CorsRegistry registry) 
        registry.addMapping("/**")//设置允许跨域的路径
                .allowedOrigins("*")//设置允许跨域请求的域名
                .allowCredentials(true)//是否允许证书 不再默认开启
                .allowedMethods("POST")//设置允许的方法"GET", "POST", "PUT", "DELETE"
                .maxAge(3600);//跨域允许时间
    */

直接在你的拦截器里添加如下代码即可

//解决中文乱码问题
@Bean
public HttpMessageConverter responseBodyConverter()
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName(“UTF-8”));
return converter;

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

以上是关于Springboot 解决返回中文乱码问题的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot返回页面乱码解决

SpringBoot使用FastJson,并解决中文乱码的问题

springboot打包发布到windows环境上中文乱码

如何解决JSON返回的中文乱码?

springMVC 解决中文乱码时使用produces报错?

Gradle 资源文件乱码解决