Spring引导:如何使ContentType标头完全可选?如果未通过,它将抛出HttpMediaTypeNotSupp



我希望我的应用程序即使在未传递ContentType标头的情况下也能工作。在我的情况下,请求主体始终是一个JSON。但是下面的代码总是抛出一个异常

org.springframework.web.HttpMediaTypeNotSupportedException:内容类型'application/x-www-form-urlencoded;charset=UTF-8不受支持

public class UserController {
@PostMapping(value = "/updateUser", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> updateUserDetails(@RequestBody User user) {
//Do something
}

这是意料之中的事,但即使在创建了自定义配置后,它也会出现同样的错误:

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

如果我传递ContentType标头(我想将其设置为纯可选(,这就可以很好地工作。请帮忙。

问题可能是因为User对象不能有任何getters/ssetters

相关内容

最新更新