pde*_*lde 6 configuration spring spring-boot
我正在将 spring-boot 从 1.3.6 更新到 2.1.3,而在响应之前有 content type application/json;charset=UTF-8,现在我得到了 iso-8859-1 的字符集。
我想要 utf 8。
我的控制器看起来像这样:
@Controller
public class MyController {
@RequestMapping(value={"/myService/{serviceId}"}, method=RequestMethod.POST, consumes="application/json")
public ResponseEntity<Void> handlePostServiceId(final InputStream requestInputStream,
@PathVariable String serviceId,
final HttpServletRequest servletRequest,) {
<$businessLogic>
return new ResponseEntity<>(new HttpHeaders(), HttpStatus.ACCEPTED);
}
Run Code Online (Sandbox Code Playgroud)
如果我包含produces= MediaType.APPLICATION_JSON_UTF8_VALUE在我的文件中,我可以让它返回 utf-8@RequestMapping但我只想设置一次,而不是为每个 API 设置。
我也尝试添加
@Override
public void configureContentNegotiation(
ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
}
Run Code Online (Sandbox Code Playgroud)
按照此处的建议转到我的 WebMvcConfigurer:https ://stackoverflow.com/a/25275291/2855921 但这破坏了我使用纯文本/文本内容类型的可用性 api。
我还确保我的请求是 UTF-8,所以它不仅仅是镜像我提供的格式。
关于如何将整个项目的字符集设置为 UTF-8 的任何想法?
将以下属性添加到application.properties文件:
对于Spring Boot 1.x
# Charset of HTTP requests and responses. Added to the "Content-Type"
# header if not set explicitly.
spring.http.encoding.charset=UTF-8
# Enable http encoding support.
spring.http.encoding.enabled=true
# Force the encoding to the configured charset on HTTP requests and responses.
spring.http.encoding.force=true
Run Code Online (Sandbox Code Playgroud)
来源:https : //docs.spring.io/spring-boot/docs/1.5.22.RELEASE/reference/html/common-application-properties.html
对于Spring Boot 2.x
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.force-response=true
Run Code Online (Sandbox Code Playgroud)
来源:https : //docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#server.servlet.encoding.charset
如果您使用的是默认对象映射器 ( Jackson),则可以使用以下简单配置强制编码:
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
jsonConverter.setDefaultCharset(StandardCharsets.UTF_8);
return jsonConverter;
}
Run Code Online (Sandbox Code Playgroud)
这里MappingJackson2HttpMessageConverter()构造函数使用公共Jackson2ObjectMapperBuilder类为对象映射器设置默认参数。
对于另一个对象映射器(Gson或Jsonb),您可以查看AllEncompassingFormHttpMessageConverter()构造函数。
自 Spring 5.2 起,Note 也MediaType.APPLICATION_JSON_UTF8被弃用了MediaType.APPLICATION_JSON。所以在测试中我更喜欢使用contentTypeCompatibleWith(...)而不是contentType(..._UTF8):
MvcResult result = mockMvc.perform(get("/api/resource"))
.andDo(print())
.andExpect(status().isOk())
// .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andReturn();
Run Code Online (Sandbox Code Playgroud)
链接:
APPLICATION_JSON_UTF8被APPLICATION_JSONin取代MappingJackson2HttpMessageConverter- https://github.com/spring-projects/spring-framework/commit/c38542739734c15e84a28ecc5f575127f25d310a"Content-Type: application/json"支持 — https://bugs.chromium.org/p/chromium/issues/detail?id=438464"charset"for "Content-Type: application/json"— https://tools.ietf.org/html/rfc7159#section-11更多示例MappingJackson2HttpMessageConverter:
| 归档时间: |
|
| 查看次数: |
12377 次 |
| 最近记录: |