spring mvc3.1 @ResponseBody注解生成大量Accept-Charset
来源:互联网 发布:点手机域名注册 编辑:程序博客网 时间:2024/04/28 17:14
Spring3 MVC使用@ResponseBody后会产生很大的响应头(Accept-Charset会达到4K+),原因在于默认情况下StringHttpMessageConverter.writeInternal()会将所有可用字符集回写到response响应头中:问题来了
解决方式:
一般我们都会重写springs mvc的HttpMessageConverter,改为utf-8编码:
package com.goldpalm.core.spring.mvc;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.nio.charset.Charset;import java.util.ArrayList;import java.util.List;import org.springframework.http.HttpInputMessage;import org.springframework.http.HttpOutputMessage;import org.springframework.http.MediaType;import org.springframework.http.converter.AbstractHttpMessageConverter;import org.springframework.util.FileCopyUtils;/** * 重写SpringMVC的字符串转换器,使用UTF-8编码 * @since 2012-7-5 下午2:28:19 * @author Jesse Lu */public class UTF8StringHttpMessageConverter extends AbstractHttpMessageConverter<String> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private final List<Charset> availableCharsets; private boolean writeAcceptCharset = true; public UTF8StringHttpMessageConverter() { super(new MediaType("text", "plain", DEFAULT_CHARSET), MediaType.ALL); this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values()); } /** * Indicates whether the {@code Accept-Charset} should be written to any outgoing request. * <p> * Default is {@code true}. */ public void setWriteAcceptCharset(boolean writeAcceptCharset) { this.writeAcceptCharset = writeAcceptCharset; } @Override public boolean supports(Class<?> clazz) { return String.class.equals(clazz); } @SuppressWarnings("rawtypes") @Override protected String readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException { Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType()); return FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset)); } @Override protected Long getContentLength(String s, MediaType contentType) { Charset charset = getContentTypeCharset(contentType); try { return (long) s.getBytes(charset.name()).length; } catch (UnsupportedEncodingException ex) { // should not occur throw new InternalError(ex.getMessage()); } } @Override protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException { if (writeAcceptCharset) { outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets()); } Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType()); FileCopyUtils.copy(s, new OutputStreamWriter(outputMessage.getBody(), charset)); } /** * Return the list of supported {@link Charset}. * <p> * By default, returns {@link Charset#availableCharsets()}. Can be overridden in subclasses. * @return the list of accepted charsets */ protected List<Charset> getAcceptedCharsets() { return this.availableCharsets; } private Charset getContentTypeCharset(MediaType contentType) { if (contentType != null && contentType.getCharSet() != null) { return contentType.getCharSet(); } else { return DEFAULT_CHARSET; } } }
在xm中配置:注意红色圈起来的配置
<mvc:annotation-driven><mvc:message-converters><bean class="com.goldpalm.core.spring.mvc.UTF8StringHttpMessageConverter"><property name="writeAcceptCharset" value="false" /></bean></mvc:message-converters></mvc:annotation-driven>
0 0
- spring mvc3.1 @ResponseBody注解生成大量Accept-Charset
- 关于spring mvc3的注解之@ResponseBody的使用
- Spring注解@ResponseBody
- spring注解@ResponseBody
- Spring 注解 @ResponseBody
- Spring MVC3的注解
- Spring注解:@Responsebody与@RequestBody
- Spring 注解@ResponseBody,@RequestBody,@PathVariable
- Spring MVC注解之@ResponseBody
- 【spring注解解析】@RequestBody,@ResponseBody
- Spring 注解 @Responsebody与@RequestBody
- Spring注解@ResponseBody中文乱码
- spring mvc3.1版本 用注解返回json 乱码
- 解决Spring MVC3.2 @ResponseBody返回中文字符串乱码问题
- SPRING MVC3.2案例讲解--SPRING MVC3的@ResponseBody和ResponseEntity
- SPRING MVC3.2案例讲解--SPRING MVC3的@ResponseBody和ResponseEntity
- Spring 注解学习 @ResponseBody,@RequestBody,@PathVariable
- spring MVC 注解处理分析(一) @ResponseBody
- LPC2478的硬件IIC使用
- Design Pattern: Decorator
- OpenGL动画
- 超文本传输协议详解
- Linux中ftp安装与基本操作
- spring mvc3.1 @ResponseBody注解生成大量Accept-Charset
- Oracle、MySql、SQLServer 数据库中分页查询中的关键字总结
- HDU 1028 Ignatius and the Princess III 母函数
- Android开发系列(十七):读取assets目录下的数据库文件
- VC++的工程文件
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-D ( ZOJ 3822 ) Domonation
- UVaOJ 1368 - DNA Consensus String
- HDU 3398 String 组合数学+N!质因数分解 卡特兰数
- ADB源码分析(二)——adb sever的启动