小编pde*_*lde的帖子

如何使 spring boot 默认为 application/json;charset=utf-8 而不是 application/json;charset=iso-8859-1

我正在将 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 的任何想法?

configuration spring spring-boot

6
推荐指数
2
解决办法
1万
查看次数

标签 统计

configuration ×1

spring ×1

spring-boot ×1