0

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

Lets say I have an object CodeRequest that contains an attribute as follows:

@NotBlank(message = "payloadFormatIndicator.required")
@Size(max = 2, message = "payloadFormatIndicator.size")
private String payloadFormatIndicator;

My controller have the object CodeRequest as parameter as shown below:

@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.

However, I get the following error message in the log:

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

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.

I am using swagger to test the rest API and the content type is shown below: enter image description here

Any idea how I can fix it pls?

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
user1999453
  • 1,297
  • 4
  • 29
  • 65
  • How are you calling your API? From the error message it seems that your `Accept` request header does not contain any supported media type (e.g. `image/png` or `image/*` in your case). – slauth Sep 30 '21 at 07:13
  • Hi i ve updated the post and i am using swagger to test the api – user1999453 Sep 30 '21 at 07:27
  • That's not correct though… it would be better to make Swagger send the correct `Accept` header… – slauth Sep 30 '21 at 08:43
  • my apologies you are right it is not correct, is it possible to identify the media type at time of submission if error json else png? – user1999453 Sep 30 '21 at 08:55

1 Answers1

0

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.

change your code like this,

@PostMapping(value = "/dummy", produces = "application/json, image/png")
ray
  • 1,512
  • 4
  • 11