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 
阅读(...) 
评论(...) 
编辑 
收藏

spring mvc3.1 @ResponseBody注解生成大量Accept-Charset的更多相关文章

  1. [转发]SPRING MVC3.2案例讲解--SPRING MVC3的@ResponseBody和ResponseEntity

    在传统的开发过程中,我们的控制CONTROLL层通常需要转向一个JSP视图:但随着WEB2.0相关技术的崛起,我们很多时候只需要返回数据即可,而不是一个JSP页面. SPRING MVC3的@Resp ...

  2. spring 3.0 @ResponseBody注解返回中文问号乱码解决办法

    前几天给公司做项目,很久没接触java项目的我,遇到了一个问题,就是我在利用异步到控制器中查询,然后返回jaon字符串到前台,字符串中包含中文,于是我直接用了@ResponseBody注解,来返回到前 ...

  3. @ResponseBody注解和@RequestBody注解

    @ResponseBody:表示该方法的返回结果直接写入HTTP response body中一般在异步获取数据时使用, 在使用@RequestMapping后,返回值通常解析为跳转路径,加上@Res ...

  4. spring mvc3的注解@ResponseBody 自动返回jason

    第三种利用spring mvc3的注解@ResponseBody 例如: @ResponseBody @RequestMapping("/list") public List< ...

  5. spring mvc 返回乱码SpringMVC使用@ResponseBody注解返回中文字符串乱码的问题

    原文地址:https://www.cnblogs.com/fzj16888/p/5923232.html 先说一下我的经历,以及解决问题的而过程. 在使用SpringMVC的时候,最开始的时候在配置文 ...

  6. Spring 关于ResponseBody注解的作用

    //responseBody一般是作用在方法上的,加上该注解表示该方法的返回结果直接写到Http response Body中,常用在ajax异步请求中, //在RequestMapping中 ret ...

  7. SpringMVC源码剖析5:消息转换器HttpMessageConverter与@ResponseBody注解

    转自 SpringMVC关于json.xml自动转换的原理研究[附带源码分析] 本系列文章首发于我的个人博客:https://h2pl.github.io/ 欢迎阅览我的CSDN专栏:Spring源码 ...

  8. Spring MVC3返回JSON数据中文乱码问题解决(转)

    Spring MVC3返回JSON数据中文乱码问题解决 查了下网上的一些资料,感觉比较复杂,这里,我这几使用两种很简单的办法解决了中文乱码问题. Spring版本:3.2.2.RELEASE Jack ...

  9. @RequestBody, @ResponseBody 注解详解

    简介: @RequestBody 作用: i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对 ...

随机推荐

  1. Linux(CentOS)修改IP地址

    登陆连接centos系统,输入 ifconfig 可以查看到当前本机的IP地址信息 一 临时修改IP地址: 1.假如查询IP为1.118,输入 ifconfig eth0 (默认是第一个网卡) 后面接 ...

  2. HTML5 Canvas 绘制库存变化折线 增加超储告罄线

    <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...

  3. 乌云主站所有漏洞综合分析&乌云主站漏洞统计

    作者:RedFree 最近的工作需要将乌云历史上比较有含金量的漏洞分析出来,顺便对其它的数据进行了下分析:统计往往能说明问题及分析事物的发展规律,所以就有了此文.(漏洞数据抓取自乌云主站,漏洞编号从1 ...

  4. FBX导入错误 :ImportFBX Errors:

    原地址:http://www.cnblogs.com/lopezycj/archive/2012/05/16/unity3d_tuchao.html Unity3D吐槽1--FBX导入 Unity3d ...

  5. POJ 2392 Space Elevator(贪心+多重背包)

    POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木.每种积木都有一个高度h[i],一个数量num ...

  6. linux 终端分屏命令vsp(转)

    比如:某文件夹下有文件:vector.cc, substr.cc 1.使用vim命令打开任意一个文件:vim vector.cc打开第一个文件.如下图所示: 2.按:"Esc"键 ...

  7. 梳理caffe代码blob(三)

    贯穿整个caffe的就是数据blob: #ifndef CAFFE_BLOB_HPP_ #define CAFFE_BLOB_HPP_ #include <algorithm> #incl ...

  8. IOS 应用程序路径

    -- 获取资源文件目录 .app/下的图片,音频等资源文件 print("bundlePath" + NSBundle.mainBundle().bundlePath) //以下是 ...

  9. hibernate 悲观锁乐观锁

    悲观锁和乐观锁是:在事务隔离机制中设置了ReadCommited的情况下,两种可以避免不可重复读的方式.   设置成读已提交是考虑到安全和处理速度,保证并发效率,但是在这个情况下仍然需要避免不可重复读 ...

  10. /proc/meminfo详解 = /nmon analysis --MEM

    memtotal hightotal lowtotal swaptotal memfree highfree lowfree swapfree memshared cached active bigf ...