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中配置:注意红色圈起来的配置

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3Vuc2hpbmVfYmVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.goldpalm.core.spring.mvc.UTF8StringHttpMessageConverter">
<property name="writeAcceptCharset" value="false" />
</bean>
</mvc:message-
posted on
2017-05-04 18:18 
lxjshuju 
阅读(...) 
评论(...) 
编辑 
收藏

最新文章

  1. 配置使用EF常见的一些问题及解决方案
  2. Uva 10891 经典博弈区间DP
  3. solr多条件查询(一)
  4. C# webservice 编写、发布、调用
  5. mysql中FIND_IN_SET的使用方法
  6. Java学习笔记——JDK1.7的新特性。
  7. HDOJ2017字符串统计
  8. easyui源码翻译1.32+API翻译全篇导航 (提供下载源码)
  9. .NET中TextBox控件设置ReadOnly=true后台取不到值三种解决方法
  10. Matlab.NET混编技巧之——找出Matlab内置函数
  11. SVM matlab 代码详解说明
  12. myeclipse 2014 customize_Perspective 失效解决方法-有效
  13. Babel插件:@babel/plugin-transform-runtime
  14. lvm管理:扩展lv、删除pv、lv等
  15. swift便利构造函数
  16. 通过ssh实现远程登陆服务器!
  17. November 11th, 2017 Week 45th Saturday
  18. AngularJS的初步学习(1)
  19. POJ 2976 Dropping tests(分数规划)
  20. MariaDB管理系统

热门文章

  1. Python图像处理(8):边缘检測
  2. Java Volatile keyword
  3. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-人机界面如何快速调整大量控件的位置
  4. vue - check-versions.js for chalk
  5. python基础语法(四)
  6. hdu1236 排名(结构体排序)
  7. android 调用系统界面
  8. Visual studio C++ 之空控制台工程添加文件并解决头文件包含问题
  9. UE4 场景展示Demo
  10. Linux下Jenkins+git+gradle持续集成环境搭建