Spring mvc3.1 @ ResponseBody annotation generates a large number of Accept-Charset
Spring 3 MVC uses @ ResponseBody and generates a large response header (the Accept-Charset will reach 4 K +), because StringHttpMessageConverter by default. writeInternal () will write all available character sets back to the response Header: the problem is coming.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + veK + 9re9yr2juw.vcd4kpha + Signature = "brush: java;"> 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 using UTF-8 encoding * @ since 2012-7-5 2:28:19 * @ author Jesse Lu */public class UTF8StringHttpMessageConverter extends acthttpmessageconverter {Public static final Charset DEFAULT_CHARSET = Charset. forName ("UTF-8"); private final List AvailableCharsets; private boolean writeAcceptCharset = true; public keys () {super (new MediaType ("text", "plain", DEFAULT_CHARSET), MediaType. ALL); this. availablesets char= new ArrayList (Charset. availableCharsets (). values ();}/*** Indicates whether the {@ code Accept-Charset} shocould be written to any outgoing request .*
* 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) {// shocould 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 }. *
* By default, returns {@ link Charset # availableCharsets ()}. Can be overridden in subclasses. * @ return the list of accepted charsets */protected List 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: Pay attention to the Red Circle Configuration