spring - Spring REST 控制器不支持的媒体类型或没有处理程序
问题描述
如果我有这样的弹簧 REST 控制器
@PostMapping(
value = "/configurations",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public CreateConfigurationResponse createConfiguration(
@RequestBody @Valid @NotNull final CreateConfigurationRequest request) {
// do stuff
}
并且客户端在标头中使用错误的媒体类型调用此端点,Accept
然后 spring 抛出一个HttpMediaTypeNotAcceptableException
. 然后我们的异常处理程序捕获它并构造一个Problem
(rfc-7807) 错误响应
@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
public class HttpMediaTypeExceptionHandler extends BaseExceptionHandler {
@ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
public ResponseEntity<Problem> notAcceptableMediaTypeHandler(final HttpMediaTypeNotAcceptableException ex,
final HttpServletRequest request) {
final Problem problem = Problem.builder()
.withType(URI.create("...."))
.withTitle("unsupported media type")
.withStatus(Status.NOT_ACCEPTABLE)
.withDetail("...error stuff..")
.build();
return new ResponseEntity<>(problem, httpStatus);
}
但是由于Problem
错误响应应该使用媒体类型发回,因此application/problem+json
spring 将其视为不可接受的媒体类型并HttpMediaTypeExceptionHandler
再次调用异常处理程序并说该媒体类型是不可接受的。
在 Spring 中有没有办法停止第二个循环进入异常处理程序,即使接受标头不包含application/problem+json
媒体类型,它也只会返回它?
解决方案
奇怪的是,当我从这里更改 return 语句时它开始工作:
return new ResponseEntity<>(problem, httpStatus);
对此:
return ResponseEntity
.status(httpStatus)
.contentType(MediaType.APPLICATION_PROBLEM_JSON)
.body(problem);
我不确定这是如何使它工作的,但确实如此。
推荐阅读
- python - 你如何在python中对一个负数求平方
- spring - 如何通过在应用程序启动时初始化的属性 bean 提供 Spring 引导属性
- python - 从 cmd 或 powershell 或 cygwin 运行时 Python 无法加载 dll 但在 Git Bash 中工作
- c - 循环后的scanf
- sas - SAS 输入和信息操作顺序
- delphi - Delphi 10.3 中使用 HTTPRIO 的基本身份验证
- sql - 如何查询 Azure Data Lake Store
- getstream-io - 链接到顶级配置文件的配置文件
- java - 如何将元组添加为(键,元组)映射的值?
- javascript - 在 Web 视图中将 Swift 变量传递给 JavaScript