springmvc使用@ResponseBody返回json乱码解决方法
2024-10-19 11:48:26
1.springmvc 3.2以上的版本解决乱码的方法:
第一步:在配置中加入:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false" />
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
第二步:需要的加入的jackson jar如下:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.1.0</version>
</dependency>
2.springmvc 3.1解决中文乱码的方法:
第一步:在配置文件中加入以下代码:
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.util.spring.UTF8StringHttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven>
第二步:自定义一个编码转换类:
public class UTF8StringHttpMessageConverter extends
AbstractHttpMessageConverter<String> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private final List<Charset> availableCharsets; public UTF8StringHttpMessageConverter() {
this(DEFAULT_CHARSET);
} public UTF8StringHttpMessageConverter(Charset defaultCharset) {
super(new MediaType("text", "plain", defaultCharset), MediaType.ALL);
this.availableCharsets = new ArrayList<Charset>(Charset .availableCharsets().values());
} @Override
protected boolean supports(Class<?> clazz) {
return String.class.equals(clazz);
} @Override
protected String readInternal(Class<? extends String> clazz,
HttpInputMessage inputMessage) throws IOException,
HttpMessageNotReadableException {
MediaType contentType = inputMessage.getHeaders().getContentType();
Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
return FileCopyUtils.copyToString(new InputStreamReader(inputMessage .getBody(), charset));
} @Override
protected void writeInternal(String t, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
MediaType contentType = outputMessage.getHeaders().getContentType();
Charset charset = contentType.getCharSet() != null ? contentType .getCharSet() : DEFAULT_CHARSET;
FileCopyUtils.copy(t, new OutputStreamWriter(outputMessage.getBody(), charset));
} protected List<Charset> getAcceptedCharsets() {
return this.availableCharsets;
} @Override
protected Long getContentLength(String s, MediaType contentType) {
if (contentType != null && contentType.getCharSet() != null) {
Charset charset = contentType.getCharSet();
try {
return (long) s.getBytes(charset.name()).length;
} catch (UnsupportedEncodingException ex) {
throw new InternalError(ex.getMessage());
}
} else {
return null;
}
}
}
第三步:加入需要的jackson包:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.8.8</version>
</dependency>
最新文章
- NOIP2012国王游戏
- java 字符串转成 json 数组并且遍历
- Linq-分组统计
- C# 3个线程A B C 依次打印123123123..
- 封装pdo单例模式类
- 在windows下使用vs2013编译和调试mysql源代码
- GLSL实现Interactive Fluid 流体【转】
- [转]Ubuntu下GitHub的使用
- UVa839 Not so Mobile
- android应用程序ANR定义
- 我学的是设计模式的视频教程——命令模式vs策略模式,唠嗑
- Spring boot+Thymeleaf+easyui集成:js创建组件页面报错
- http 概念
- python爬虫-基础入门-python爬虫突破封锁
- Opatch使用细则
- Python学习之路【第一篇】-Python简介和基础入门
- Linux登录超时自动退出处理办法
- Thinkphp时间转换与统计的问题
- Git实用教程
- 设计模式 笔记 状态模式 State