Spring mvc3.1 @ ResponseBody annotation generates a large number of Accept-Charset

Source: Internet
Author: User

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



 
  
   
    
   
  
 





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.