基于 Spring 的 REST API,发现响应头里的 Accept-Charset 非常巨大
引用
Accept-Charset: big5, big5-hkscs, cesu-8, euc-jp, euc-kr, gb18030, gb2312, gbk, ibm-thai, ibm00858, ibm01140, ibm01141, ibm01142, ibm01143, ibm01144, ibm01145, ibm01146, ibm01147, ibm01148, ibm01149, ibm037, ibm1026, ibm1047, ibm273, ibm277, ibm278, ibm280, ibm284, ibm285, ibm290, ibm297, ibm420, ibm424, ibm437, ibm500, ibm775, ibm850, ibm852, ibm855, ibm857, ibm860, ibm861, ibm862, ibm863, ibm864, ibm865, ibm866, ibm868, ibm869, ibm870, ibm871, ibm918, iso-2022-cn, iso-2022-jp, iso-2022-jp-2, iso-2022-kr, iso-8859-1, iso-8859-13, iso-8859-15, iso-8859-2, iso-8859-3, iso-8859-4, iso-8859-5, iso-8859-6, iso-8859-7, iso-8859-8, iso-8859-9, jis_x0201, jis_x0212-1990, koi8-r, koi8-u, shift_jis, tis-620, us-ascii, utf-16, utf-16be, utf-16le, utf-32, utf-32be, utf-32le, utf-8, windows-1250, windows-1251, windows-1252, windows-1253, windows-1254, windows-1255, windows-1256, windows-1257, windows-1258, windows-31j, x-big5-hkscs-2001, x-big5-solaris, x-compound_text, x-euc-jp-linux, x-euc-tw, x-eucjp-open, x-ibm1006, x-ibm1025, x-ibm1046, x-ibm1097, x-ibm1098, x-ibm1112, x-ibm1122, x-ibm1123, x-ibm1124, x-ibm1364, x-ibm1381, x-ibm1383, x-ibm300, x-ibm33722, x-ibm737, x-ibm833, x-ibm834, x-ibm856, x-ibm874, x-ibm875, x-ibm921, x-ibm922, x-ibm930, x-ibm933, x-ibm935, x-ibm937, x-ibm939, x-ibm942, x-ibm942c, x-ibm943, x-ibm943c, x-ibm948, x-ibm949, x-ibm949c, x-ibm950, x-ibm964, x-ibm970, x-iscii91, x-iso-2022-cn-cns, x-iso-2022-cn-gb, x-iso-8859-11, x-jis0208, x-jisautodetect, x-johab, x-macarabic, x-maccentraleurope, x-maccroatian, x-maccyrillic, x-macdingbat, x-macgreek, x-machebrew, x-maciceland, x-macroman, x-macromania, x-macsymbol, x-macthai, x-macturkish, x-macukraine, x-ms932_0213, x-ms950-hkscs, x-ms950-hkscs-xp, x-mswin-936, x-pck, x-sjis_0213, x-utf-16le-bom, x-utf-32be-bom, x-utf-32le-bom, x-windows-50220, x-windows-50221, x-windows-874, x-windows-949, x-windows-950, x-windows-iso2022jp
查了一下源码,发现默认的 StringHttpMessageConverter 会加上 Accept-Charset 响应头,并带上所有的字符集!这个选项需要通过设置 writeAcceptCharset 属性来关闭。
org.springframework.http.converter.StringHttpMessageConverter
private boolean writeAcceptCharset = true;
if (this.writeAcceptCharset) {
outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
}
this.availableCharsets = new ArrayList<>(Charset.availableCharsets().values());
protected List<Charset> getAcceptedCharsets() {
return this.availableCharsets;
}
也有人反映,为什么 writeAcceptCharset 属性不初始化为fale,
https://jira.spring.io/browse/SPR-7316
源码中也注释了,可以通过子类覆盖 getAcceptedCharsets() 方法来返回该有的charsets。
/**
* Return the list of supported {@link Charset}s.
* <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;
}
XML配置:
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"></constructor-arg>
<property name="writeAcceptCharset" value="false"/>
</bean>
Java配置:
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
stringHttpMessageConverter.setWriteAcceptCharset(false);
SpringBoot已经通过自动配置关闭了这个选项
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration
@Bean
@ConditionalOnMissingBean
public StringHttpMessageConverter stringHttpMessageConverter() {
StringHttpMessageConverter converter = new StringHttpMessageConverter(this.encodingProperties.getCharset());
converter.setWriteAcceptCharset(false);
return converter;
}
分享到:
相关推荐
赠送原API文档:spring-jdbc-5.3.15-javadoc.jar; 赠送源代码:spring-jdbc-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-jdbc-5.3.15.pom; 包含翻译后的API文档:spring-jdbc-5.3.15-javadoc-API文档-...
赠送原API文档:spring-webflux-5.3.15-javadoc.jar; 赠送源代码:spring-webflux-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-webflux-5.3.15.pom; 包含翻译后的API文档:spring-webflux-5.3.15-javadoc...
implementation 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE' } ``` 2. 创建REST Template实例:在需要使用的地方初始化REST Template对象。 ```java RestTemplate restTemplate = ...
Spring Boot REST API Tutorial Master 是一个基于Spring Boot框架的示例项目,它展示了如何构建RESTful Web服务。这个项目是为那些想要学习如何在Spring Boot环境中创建高效、可伸缩的API的人准备的。REST...
本篇文章将围绕"spring-boot-api-project-seed-master.zip"这个压缩包文件,深入探讨Spring Boot的核心特性、API设计原则以及如何通过这个项目种子进行实战演练。 1. **Spring Boot概述** Spring Boot是由Pivotal ...
spring-data-rest-core-3.6.0.jar
activiti-spring-boot-starter-basic-6.0.0适配springboot2.1.2
本项目"restapi-version"是一个演示如何在Spring MVC中实现REST API版本管理的雏形,旨在帮助开发者理解并掌握这一关键概念。 首先,让我们详细了解一下REST API的版本控制。在API设计中,随着业务需求的变化和功能...
克隆应用程序git clone https://github.com/callicoder/spring-boot-mysql-rest-api-tutorial.git 2.创建Mysql数据库create database notes_app 3.根据您的安装更改mysql用户名和密码打开src/main/resources/...
spring-data-rest-core-2.3.0.RELEASE.jar
cd spring-webflux-reactive-rest-api-demo mvn package java -jar target/webflux-demo-0.0.1-SNAPSHOT.jar 另外,您也可以使用- mvn spring-boot:run 服务器将从。 探索Rest API 该应用程序定义以下REST API ...
spring-rest-exception-handler, 一个方便的RESTful api Spring MVC 异常 spring REST异常处理程序 这个项目的目的是为for提供一个方便的异常处理程序( 解析器),它满足了错误响应的最佳实践。 处理自定义异常。...
包含spring 3.0.5的所有jar文件: org.springframework.aop-3.0.5.RELEASE.jar org.springframework.asm-3.0.5.RELEASE.jar org.springframework.aspects-3.0.5.RELEASE.jar org.springframework.beans-3.0.5.RELEASE...
《dbApi-spring-boot-starter实战指南:基于Java的数据库API启动器应用示例》 在现代软件开发中,Spring Boot已经成为Java企业级应用的首选框架,它简化了配置并提供了快速开发的能力。dbApi-spring-boot-starter是...
Api-auth0-spring-security-api-sample.zip,Asv0集成JavaSpring和Spring Security REST API Services Auth0API示例的示例:使用Spring Bug和Spring Security,一个api可以被认为是多个软件设备之间通信的指导手册。...
spring-webmvc-struts.jar对Struts和Spring整合时需要用到的包
安装git clone https://github.com/monogramm/spring-rest-api-starter.git cd spring-rest-api-starter 最后,根据需要编辑application.properties的属性(名称,数据库,邮件等),然后编译项目。 mvn clean ...
cd spring-boot-file-upload-download-rest-api-example mvn spring-boot:run 而已! 可以从http://localhost:8080访问该应用程序。 您也可以以jar的形式打包应用程序,然后像这样运行jar文件- mvn clean package...
<artifactId>spring-webmvc ${spring.version} <!-- 其他依赖项,如Spring核心库等 --> <groupId>org.springframework <artifactId>spring-core ${spring.version} <!-- 添加日志库,例如Logback --> ...
org.springframework.spring-library-3.1.RELEASE.libd org.springframework.test-3.1.RELEASE.jar org.springframework.transaction-3.1.RELEASE.jar org.springframework.web.portlet-3.1.RELEASE.jar org....