SpringBoot返回页面乱码解决
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返回页面乱码解决的更多相关文章
- springMvc 中返回字符串 乱码解决
/** * produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8" 乱码解决 * @param callback * ...
- springboot返回页面
1.使用@Controller注解: @Controller必须配合模板 先导入依赖: <dependency> <groupId>org.springframework.bo ...
- jquery ajax返回html乱码解决
情景: index.html 使用如下代码访问另外一个html片段 1 2 3 4 5 6 7 8 9 10 11 12 13 function rewritedata(id){ var uu ...
- springmvc使用@ResponseBody返回json乱码解决方法
1.springmvc 3.2以上的版本解决乱码的方法: 第一步:在配置中加入: <mvc:annotation-driven> <mvc:message-converters re ...
- Springmvc返回信息乱码解决
恩...基本上所有的配置信息都弄上了,但是还是乱码,最后在方法上面添加了下面的参数,就完美解决了: @RequestMapping(value="/action.action",m ...
- fiddler 之 返回数据乱码解决方法
1.有时用fiddler抓包, 发现抓到的包, 发送数据和返回数据都是乱码, 怎么办? 直接上图 (这办法不是100%成功的) 方法一: 方法二:
- tomcat jsp页面乱码解决
浏览器接收服务器响应的中文参数: JSP页面中告诉浏览器使用什么编码: <%@ page language="java" contentType="text/htm ...
- Flex用HTTPService调用servlet返回中文乱码解决
servlet中使用URLEncoder.encode对输出内容进行编码 Flex中使用decodeURIComponent进行解码
- requests返回页面乱码
req=requests.post(domain,params,json=None) req=req.content.decode()
随机推荐
- substr 字符串截取
<!DOCTYPE html> <html> <body> <?php echo substr("Hello world",0,10).& ...
- openstack核心组件--neutron网络服务2(4)
一.虚拟机获取 ip: 用 namspace 隔离 DHCP 服务 Neutron 通过 dnsmasq 提供 DHCP 服务,而 dnsmasq 通过 Linux Network Names ...
- RobotFramework的安装
Robot Framework自动化测试框架+可视化编辑工具RIDE+Selenium2这是规范的webAPI. 一通过下载安装包安装 1)RF 框架是基于 Python 语言的,所以一定要有 Pyt ...
- guns系统部署方式及常见问题
项目框架:guns 开发IDE:Idea 2018.1 两种打包方式:war 和jar. 1.正常打包的姿势 1.1按照下图修改为你想到打包的方式. 1.2 执行打包 clean packa ...
- centos7 httpd配置
centos7 httpd配置 标签(空格分隔): 未分类 隐藏server信息 修改httpd.conf 设置,添加如下两行 ServerSignature Off ServerTokens Pro ...
- Golang结构体struct的使用(结构体嵌套, 匿名结构体等)
转自: https://studygolang.com/articles/11313 golang中是没有class的,但是有一个结构体struct,有点类似,他没有像java,c++中继承的概念,但 ...
- 2019牛客暑期多校训练营(第六场)-D Move
题目链接:https://ac.nowcoder.com/acm/contest/886/D 题意:给n个物品,每个物品有一个体积值,K个箱子,问箱子的最小体积为多少可将物品全部装下. 思路:比赛时一 ...
- matplotlib库绘制散点图
假设通过爬虫你获取到了北京2016年3,10月份每天白天的最高气温(分别位于列表a,b),那么此时如何寻找出气温随时间(天)变化的某种规律? a = [11,17,16,11,12,11,12,6,6 ...
- union和in哪个效率高
一直都认为是in的效率要高,但是这次确有点蒙圈. SELECT * FROM runinfo WHERE status in (0,2,1,3,4,7,9,10); 这个查询的效率是,经常是1秒多. ...
- 代码优化:Java编码技巧之高效代码50例
出处: Java编码技巧之高效代码50例 1.常量&变量 1.1.直接赋值常量值,禁止声明新对象 直接赋值常量值,只是创建了一个对象引用,而这个对象引用指向常量值. 反例: Long i = ...