Spring3 MVC使用@ResponseBody产生很大的响应头
2012-09-08 18:08
519 查看
Spring3 MVC使用@ResponseBody后会产生很大的响应头(Accept-Charset会达到4K+),原因在于默认情况下StringHttpMessageConverter.writeInternal()会将所有可用字符集回写到response响应头中:
/** * 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 be overridden by setting the {@link * #setSupportedMediaTypes(java.util.List) supportedMediaTypes} property. * * @author Arjen Poutsma * @since 3.0 */ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<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 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); } @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 && contentType.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 (writeAcceptCharset) { 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 be overridden in subclasses. * * @return the list of accepted charsets */ protected List<Charset> getAcceptedCharsets() { return this.availableCharsets; } }解决办法:Spring3 MVC使用@ResponseBody的乱码问题及解决办法
相关文章推荐
- Spring MVC+hibernate 使用@ResponseBody返回json报500 (Internal Server Error)
- Spring MVC @ResponseBody响应中文乱码
- SpringMVC @RequestBody与@ResponseBody注解的使用
- springMVC @responseBody注解的使用
- Spring3 MVC使用@ResponseBody的乱码问题及解决办法
- Spring3 MVC使用@ResponseBody的乱码问题及解决办法
- Spring MVC @ResponseBody注解返回响应流时中文乱码问题的解决
- SpringMVC @ResponseBody的使用
- Spring3 MVC使用@ResponseBody的乱码问题及解决办法
- Spring MVC @ResponseBody响应中文乱码
- Spring3 MVC使用@ResponseBody的乱码问题及解决办法
- Spring3 MVC使用@ResponseBody的乱码问题及解决办法
- @SpringMVC @ResponseBody的使用
- Spring MVC @ResponseBody注解返回响应流时中文乱码问题的解决
- Spring MVC @ResponseBody注解返回响应流时中文乱码问题的解决
- springmvc Spring3 MVC @ResponseBody返回,jquery ajax调用中文乱码问题解决
- Spring MVC ResponseBody
- 解决spring-mvc @responseBody注解返回json 乱码问题
- SpringMVC @ResponseBody 返回json date 转换
- springMVC ResponseBody