Spring mvc3.1 @ ResponseBody annotation generates a large number of Accept-Charset, responsebody
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.
Solution:
Generally, we will rewrite the HttpMessageConverter of springs mvc 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; impo Rt org. springframework. http. converter. abstractHttpMessageConverter; import org. springframework. util. fileCopyUtils;/*** rewrite the SpringMVC string converter, use UTF-8 encoding * @ since 2012-7-5 2:28:19 * @ author Jesse Lu */public class UTF8StringHttpMessageConverter extends acthttpmessageconverter <String> {public static final Charset DEFAULT_CHARSET = Charset. forName ("UTF-8"); private final List <Charset> availableCha Rsets; 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} shoshould be written to any outgoing request. * <p> * Default is {@ code true }. */public void setWri TeAcceptCharset (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. ge THeaders (). 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 be overridden in subclasses. * @ return the list of accep Ted 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: Pay attention to the Red Circle 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>
@ ResponseBody what is the annotation in spring?
@ ResponseBody indicates the body of the response. That is, the VIEW presentation layer module is not required to directly display the content on the client.
@ RequestBody PmsAttendance attendance,
HttpServletRequest request
This means that PmsAttendance is the subject content of the response.
Java Development, we recommend you a lightweight java database development framework, JDiy. Baidu search JDiy go to the official website to learn more.
What is @ ResponseBody in the method below spring annotation @ ResponseBody?
The return value of your code is used as the content of the http request to play to the client.
To put it bluntly, you can accept the data sent by the background directly as json on the foreground.