SPRING3 MVC uses @responsebody to produce very large response headers

Source: Internet
Author: User

SPRING3 MVC uses @responsebody to produce a very large response header (Accept-charset will reach 4k+) because Stringhttpmessageconverter.writeinternal () by default All available character sets are written back to the response response header:

/** * Implementation of {@link Httpmessageconverter} that can read and write strings. 
 * * <p>by default, this converter supports all media types (<code>*/*</code>), and writes with a {@code * Content-type} of {@code text/plain}.
 This can is overridden by setting the {@link * #setSupportedMediaTypes (java.util.List) Supportedmediatypes} property. * * @author Arjen Poutsma * @since 3.0 * * public class Stringhttpmessageconverter extends abstracthttpmessageconverter& Lt

	string> {public static final Charset Default_charset = Charset.forname ("iso-8859-1");

	Private final list<charset> availablecharsets;

	Private Boolean writeacceptcharset = true;
		Public Stringhttpmessageconverter () {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); } @Override protected String readinternal (Class clazz, Httpinputmessage inputmessage) throws IOException {mediatype
		ContentType = Inputmessage.getheaders (). getContentType (); Charset Charset = Contenttype.getcharset ()! = null?
		Contenttype.getcharset (): Default_charset;
	Return filecopyutils.copytostring (New InputStreamReader (Inputmessage.getbody (), CharSet)); } @Override protected Long getcontentlength (String S, mediatype ContentType) {if (ContentType! = null && con
			Tenttype.getcharset () = null) {Charset Charset = Contenttype.getcharset ();
			try {return (long) s.getbytes (Charset.name ()). length; } catch (Unsupportedencodingexception ex) {//should not occur throw new Internalerror (ex.getMessage ());
		}} else {return null; }} @Override protected void writeinternal (String s, Httpoutputmessage outputmessage) throws IOException {if (write
		Acceptcharset) {outputmessage.getheaders (). Setacceptcharset (Getacceptedcharsets ());
		} MediaType ContentType = Outputmessage.getheaders (). getContentType (); Charset Charset = Contenttype.getcharset ()! = null?
		Contenttype.getcharset (): Default_charset;
	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.avai
	Lablecharsets; }

Workaround: Spring3 MVC using @responsebody garbled problem and solution

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.