spring mvc3.1 @ResponseBody註解產生大量Accept-Charset,responsebody

來源:互聯網
上載者:User

spring mvc3.1 @ResponseBody註解產生大量Accept-Charset,responsebody

Spring3 MVC使用@ResponseBody後會產生很大的回應標頭(Accept-Charset會達到4K+),原因在於預設情況下StringHttpMessageConverter.writeInternal()會將所有可用字元集回寫到response回應標頭中:問題來了


解決方式:

一般我們都會重寫springs mvc的HttpMessageConverter,改為utf-8編碼:

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;/** * 重寫SpringMVC的字串轉換器,使用UTF-8編碼 * @since 2012-7-5 下午2:28:19 * @author Jesse Lu */public class UTF8StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {        public static final Charset DEFAULT_CHARSET = Charset.forName("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 be 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 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) {            // should 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}.     * <p>     * By default, returns {@link Charset#availableCharsets()}. Can be 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;        }    }    }

在xm中配置:注意紅色圈起來的配置



<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這個在spring註解是什

@ResponseBody表示響應的主體。即不需要VIEW展現層模組,直接顯示到用戶端的內容。
@RequestBody PmsAttendance attendance,
HttpServletRequest request
這個的意思是,PmsAttendance這個是響應的主體內容。

java開發,推薦你一個超輕量級的java資料庫開發架構,JDiy。百度搜尋JDiy進官網瞭解更多內容。
 
spring註解@ResponseBody 下面方法中的@ResponseBody是什?

就是將你代碼return的值作為http請求的內容發揮用戶端
說白了就是你可以在前台直接當成json來接受後台發送的資料
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.