Spring3 MVC uses @responsebody to produce very large response headers (accept-charset will reach 4k+). The reason is that stringhttpmessageconverter.writeinternal () writes all available character sets back to the response response header by default: The problem is.
Solution:
In general, we will rewrite Springs MVC's httpmessageconverter. Change to UTF-8 encoding:
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;/ * * Rewrite the SPRINGMVC string converter. Use UTF-8 encoding * @since 2012-7-5 PM 2:28:19 * @author Jesse Lu */public class Utf8stringhttpmessageconverter extends Abstracthtt pmessageconverter<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 is 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 inputme Ssage) 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 = Getconte Nttypecharset (ContentType); try {return (long) s.getbytes (Charset.name ()). length; } catch (Unsupportedencodingexception ex) {//should not occur throw new Internalerror (EX.GETMESSAG E ()); }} @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 is overridden in subclasses. * @return The list of accepted charsets */protected list<charset> getacceptedcharsets () {return this . availablecharsets; } Private Charset Getcontenttypecharset (mediatype contentType) {if (ContentType! = null && Contentt Ype.getcharset ()! = null) {return contenttype.getcharset (); } else {return default_charset; } } }
Configure in XM: note the red-circled configuration
<mvc:annotation-driven><mvc:message-converters><bean class= " Com.goldpalm.core.spring.mvc.UTF8StringHttpMessageConverter "><property name=" Writeacceptcharset "value=" False "/></bean></mvc:message-
Spring mvc3.1 @ResponseBody Annotations Generate a large number of Accept-charset