SpringBoot,在做全局异常处理的时候,返回中文字符串时,出现乱码情况,网上查阅资料之后,解决方式如下所示,自定义WebConfiguration继承WebMvcConfigurationSupport类(用的是SpringBoot2.0)。
(之前返回json串时遇到乱码问题,是在@RequestMapping中添加了 produces=“application/json;charset=utf-8”。 但是在处理全局异常信息是,没有@RequestMapping这个注解去添加该属性(也许是我学的太浅,还没找到吧),而且这中做法还限制了请求的数据类型。于是继续寻找合适的方法,最终找到此解决方案)

 import java.nio.charset.Charset;
import java.util.List; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import com.fasterxml.jackson.databind.ObjectMapper; import ch.qos.logback.classic.pattern.MessageConverter; /**
* 解决页面返回的中文乱码。
* 自定义消息转换器:自定义WebConfiguration继承WebMvcConfigurationSupport类
* @author Administrator
* @date 2018年10月18日上午12:34:22
*/
@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport{ //1.这个为解决中文乱码
@Bean
public HttpMessageConverter<String> responseBodyConverter() {
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
return converter;
} //2.1:解决中文乱码后,返回json时可能会出现No converter found for return value of type: xxxx
//或这个:Could not find acceptable representation
//解决此问题如下
public ObjectMapper getObjectMapper() {
return new ObjectMapper();
} //2.2:解决No converter found for return value of type: xxxx
public MappingJackson2HttpMessageConverter messageConverter() {
MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(getObjectMapper());
return converter;
} @Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//解决中文乱码
converters.add(responseBodyConverter()); //解决: 添加解决中文乱码后的配置之后,返回json数据直接报错 500:no convertter for return value of type
//或这个:Could not find acceptable representation
converters.add(messageConverter());
} }

SpringBoot返回页面乱码解决的更多相关文章

  1. springMvc 中返回字符串 乱码解决

    /** * produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8" 乱码解决 * @param callback * ...

  2. springboot返回页面

    1.使用@Controller注解: @Controller必须配合模板 先导入依赖: <dependency> <groupId>org.springframework.bo ...

  3. jquery ajax返回html乱码解决

    情景: index.html 使用如下代码访问另外一个html片段 1 2 3 4 5 6 7 8 9 10 11 12 13 function rewritedata(id){     var uu ...

  4. springmvc使用@ResponseBody返回json乱码解决方法

    1.springmvc 3.2以上的版本解决乱码的方法: 第一步:在配置中加入: <mvc:annotation-driven> <mvc:message-converters re ...

  5. Springmvc返回信息乱码解决

    恩...基本上所有的配置信息都弄上了,但是还是乱码,最后在方法上面添加了下面的参数,就完美解决了: @RequestMapping(value="/action.action",m ...

  6. fiddler 之 返回数据乱码解决方法

    1.有时用fiddler抓包, 发现抓到的包, 发送数据和返回数据都是乱码, 怎么办?   直接上图  (这办法不是100%成功的) 方法一: 方法二:

  7. tomcat jsp页面乱码解决

    浏览器接收服务器响应的中文参数: JSP页面中告诉浏览器使用什么编码: <%@ page language="java" contentType="text/htm ...

  8. Flex用HTTPService调用servlet返回中文乱码解决

    servlet中使用URLEncoder.encode对输出内容进行编码 Flex中使用decodeURIComponent进行解码

  9. requests返回页面乱码

    req=requests.post(domain,params,json=None) req=req.content.decode()

随机推荐

  1. consul ocelot

    consul配置完成后 新建.netcoreapi项目, nuget安装ocelot 添加多个配置文件,.netcore中会自动合并为一个文件,global配置总的配置,其他为各个项目的配置 Serv ...

  2. 基于spring reactor3构建的即时通讯api。

    欢迎阅读Rmessage文档 技术栈 Rmessage是采用Reactor3,基于reactor-netty项目构建的实时消息推送api. 什么是Reactor3? Reactor 是一个用于JVM的 ...

  3. centos7安装kvm及kvm管理

    一.安装kvm 查看CPU是否支持虚拟化  grep -E 'svm|vmx' /proc/cpuinfo - vmx is for Intel processors - svm is for AMD ...

  4. 4. Linux管理命令

    df.du:磁盘.空间相关的命令 df -h 以直观的方式显示磁盘分区使用状况 df test 查询test属于哪个分区   du du -h 以直观方式显示文件夹目录的使用状况 du -s 只显示当 ...

  5. 【问题案例】K8S-Master修改IP地址之后,重新初始化的方法。

    使用kubeadm命令,执行:kubeadm reset 重新执行初始化:kubeadm init --kubernetes-version=v1.14.1 --pod-network-cidr=10 ...

  6. python之xml数据解析

    因为项目需求需要查询一些网站的ALEXA排名,百度后得到的方法是,访问http://data.alexa.com/data?cli=10&dat=snbamz&url=%YOURURL ...

  7. 整合AD RMS与EX 2010。

    1.点击开始菜单, 选择所有程 序,展开 Mi cros oft  Excha nge  Server  2010 ,打开Excha nge Ma na gement Cons ol e,选择收件人配 ...

  8. 二、Linux用户身份

    0.介绍: 管理员UID为0:系统的管理员用户. 系统用户UID为1-999: Linux系统为了避免因某个服务程序出现漏洞而被黑客提权至整台服务器,默认服务程序会有独立的系统用户负责运行,进而有效控 ...

  9. VS2019 安装并破解 DevExpress 19.2 插件

    0.写在最前 本人vs版本为:     1.下载DevExpress 最新版本插件程序 官网下载试用30天地址: https://www.devexpress.com/products/net/con ...

  10. 【计算机视觉】背景建模之PBAS

    本文是根据M. Hofmann等人在2012年的IEEE Workshop on Change Detection上发表的"Background Segmentation with Feed ...