gpt4 book ai didi

Spring boot - 如果请求中不存在,则设置默认的 Content-type header

转载 作者:行者123 更新时间:2023-12-02 04:31:34 26 4
gpt4 key购买 nike

我遇到了以下问题:假设我有时收到没有 Content-type 的 POST 请求 header 集。在这种情况下,我想假设 Content-type=application/json默认情况下。

我可以以某种方式使用 Spring 启动功能而不使用过滤器来实现这一点吗?

谢谢

最佳答案

从 Spring Boot 2.x 开始,您需要创建一个扩展 WebMvcConfigurer 接口(interface)的类,例如:

@Configuration
class WebMvcConfiguration implements WebMvcConfigurer {
@Override
public void configureContentNegotiation( ContentNegotiationConfigurer configurer )
{
configurer.defaultContentType( MediaType.APPLICATION_JSON );
}
}

在 1.x 下,您可以使用现已弃用的 WebMvcConfigurerAdapter 执行相同的操作。

这将影响请求和响应主体,因此如果您没有显式设置“produces”参数,并且您想要的不是 application/json,它将被强制转换为 application/json。

关于Spring boot - 如果请求中不存在,则设置默认的 Content-type header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48216052/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com