spring mvc3.1 @ResponseBody注解生成大量Accept-Charset

2024-02-20 21:08

本文主要是介绍spring mvc3.1 @ResponseBody注解生成大量Accept-Charset,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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;}@Overridepublic boolean supports(Class<?> clazz) {return String.class.equals(clazz);}@SuppressWarnings("rawtypes")@Overrideprotected String readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException {Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType());return FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset));}@Overrideprotected Long getContentLength(String s, MediaType contentType) {Charset charset = getContentTypeCharset(contentType);try {return (long) s.getBytes(charset.name()).length;} catch (UnsupportedEncodingException ex) {// should not occurthrow new InternalError(ex.getMessage());}}@Overrideprotected 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>





这篇关于spring mvc3.1 @ResponseBody注解生成大量Accept-Charset的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/729547

相关文章

MybatisPlus 多数据源切换@DS注解失效问题解决

《MybatisPlus多数据源切换@DS注解失效问题解决》在业务开发中使用到了多数据源,遇到了@DS注解失效问题,有两个场景使用到同一个@DS的查询方法,下面就来介绍一下该问题的解决,感兴趣的可以... 在业务开发中使用到了多数据源,遇到了@DS注解失效问题,有两个场景使用到同一个@DS的查询方法,一个正

springboot控制bean的创建顺序

《springboot控制bean的创建顺序》本文主要介绍了spring-boot控制bean的创建顺序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1、order注解(不一定有效)2、dependsOn注解(有效)3、提前将bean注册为Bea

Java中的ConcurrentBitSet使用小结

《Java中的ConcurrentBitSet使用小结》本文主要介绍了Java中的ConcurrentBitSet使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、核心澄清:Java标准库无内置ConcurrentBitSet二、推荐方案:Eclipse

java中的Supplier接口解析

《java中的Supplier接口解析》Java8引入的Supplier接口是一个无参数函数式接口,通过get()方法延迟计算结果,它适用于按需生成场景,下面就来介绍一下如何使用,感兴趣的可以了解一下... 目录1. 接口定义与核心方法2. 典型使用场景场景1:延迟初始化(Lazy Initializati

Java中ScopeValue的使用小结

《Java中ScopeValue的使用小结》Java21引入的ScopedValue是一种作用域内共享不可变数据的预览API,本文就来详细介绍一下Java中ScopeValue的使用小结,感兴趣的可以... 目录一、Java ScopedValue(作用域值)详解1. 定义与背景2. 核心特性3. 使用方法

spring中Interceptor的使用小结

《spring中Interceptor的使用小结》SpringInterceptor是SpringMVC提供的一种机制,用于在请求处理的不同阶段插入自定义逻辑,通过实现HandlerIntercept... 目录一、Interceptor 的核心概念二、Interceptor 的创建与配置三、拦截器的执行顺

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

Spring Boot 中 RestTemplate 的核心用法指南

《SpringBoot中RestTemplate的核心用法指南》本文详细介绍了RestTemplate的使用,包括基础用法、进阶配置技巧、实战案例以及最佳实践建议,通过一个腾讯地图路线规划的案... 目录一、环境准备二、基础用法全解析1. GET 请求的三种姿势2. POST 请求深度实践三、进阶配置技巧1

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

Spring Boot 处理带文件表单的方式汇总

《SpringBoot处理带文件表单的方式汇总》本文详细介绍了六种处理文件上传的方式,包括@RequestParam、@RequestPart、@ModelAttribute、@ModelAttr... 目录方式 1:@RequestParam接收文件后端代码前端代码特点方式 2:@RequestPart接