Spring3 MVC uses @responsebody to produce a large response header (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.
How to resolve:
In general, we will rewrite Springs MVC's httpmessageconverter and change it 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 SPRINGMVC string converter, use UTF-8 encoding * @since 2012-7-5 PM 2:28:19 * @author Jesse Lu */public class Utf8stringhttpmessageconve Rter extends abstracthttpmessageconverter<string> {public static final Charset Default_charset = charset.for Name ("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 && contenttype.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-converters></mvc:annotation-driven>
Spring mvc3.1 @ResponseBody Annotations Generate a large number of Accept-charset