[英]Spring Boot Rest Controller endpoint exception: HttpMediaTypeNotAcceptableException
I am experiencing an issue while a mandatory field is not filled, the following exception is displayed in the logs:我在未填写必填字段时遇到问题,日志中显示以下异常:
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示
Lets say I have an object CodeRequest that contains an attribute as follows:假设我有一个 object CodeRequest,其中包含如下属性:
@NotBlank(message = "payloadFormatIndicator.required")
@Size(max = 2, message = "payloadFormatIndicator.size")
private String payloadFormatIndicator;
My controller have the object CodeRequest as parameter as shown below:我的 controller 将 object CodeRequest 作为参数,如下所示:
@PostMapping(value = "/dummy", produces = MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<BufferedImage> generateQRCode(@Valid @RequestBody CodeRequest paymentRequest) throws Exception {
log.debug("generateQRCode with the following request {}", paymentRequest);
return ResponseEntity.status(HttpStatus.OK).body(ipsPaymentService.generateQRCode(paymentRequest));
}
When I leave the mandatory field payloadFormatIndicator
empty I expect to get an error message that payloadFormatIndicator.required
is required in my response.当我将必填字段payloadFormatIndicator
留空时,我希望收到一条错误消息,指出我的响应中需要payloadFormatIndicator.required
。
However, I get the following error message in the log:但是,我在日志中收到以下错误消息:
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示
My exception handler is shown below:我的异常处理程序如下所示:
@Slf4j
@ControllerAdvice
public class RestControllerExceptionHandler extends ResponseEntityExceptionHandler {
@Override
public ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException exception, HttpHeaders headers,
HttpStatus status, WebRequest request) {
log.error(exception.getMessage(), exception);
ExceptionResponse exceptionResponse = new ExceptionResponse(HttpStatus.BAD_REQUEST,
exception.getBindingResult().getAllErrors().get(0).getDefaultMessage());
return new ResponseEntity<>(exceptionResponse, new HttpHeaders(), exceptionResponse.getHttpStatus());
}
It looks like because the method generateQRCode is returning ResponseEntity<BufferedImage>
it is causing this issue because for the other methods on my controller, the exception handling is working fine.看起来是因为方法 generateQRCode 正在返回ResponseEntity<BufferedImage>
它导致了这个问题,因为对于我的 controller 上的其他方法,异常处理工作正常。
I am using swagger to test the rest API and the content type is shown below:我正在使用 swagger 测试 rest API 内容类型如下所示:
Any idea how I can fix it pls?知道我该如何解决吗?
The issue is because of producer media-type.问题是因为生产者媒体类型。 The response only accept image/png
, yet when there is an error media-type is application/json
.响应仅接受image/png
,但当出现错误时,媒体类型为application/json
。
change your code like this,像这样改变你的代码,
@PostMapping(value = "/dummy", produces = "application/json, image/png")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.