9

I´m having some issues when returning some errors from a rest WebService.

Making a request with the header {"Accept":"application/octet-stream"} (the service returns a document ResponseEntity<InputStreamResource> if all the process goes well).

When all the process goes well the document is downloaded fine, but when an error is occurred and the code jumps to the @ControllerAdvice and tries to return a JSON error. Here comes the problem, when trying to return the JSON springs crashes:

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

Here is a example of some code:

Controller

@RequestMapping(value = "/test", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE })
    public ResponseEntity<CustomError> test() throws Exception {
        throw new Exception();
    }

ControllerAdvice

@ControllerAdvice
public class ExceptionHandlerAdvice {
    private static final Logger logger = LogManager.getLogger(ExceptionHandlerAdvice.class);

    @ExceptionHandler({Exception.class,Throwable.class})
    @ResponseBody
    public ResponseEntity<CustomError> handleUnhandledException(Exception exception) {
        CustomError error = new CustomError(exception.getMessage());
        return new ResponseEntity<CustomError>(error, HttpStatus.INTERNAL_SERVER_ERROR);
    }

}

CustomError:

public class CustomError {

    private String errorDescription;

    public CustomError(String errorDescription) {
        super();
        this.errorDescription = errorDescription;
    }

    public String getErrorDescription() {
        return errorDescription;
    }

    public void setErrorDescription(String errorDescription) {
        this.errorDescription = errorDescription;
    }

}

I´ve also tried returning new headers on @controllerAdvice

@ExceptionHandler({Exception.class,Throwable.class})
  @ResponseBody
  public ResponseEntity<CustomError> handleUnhandledException(Exception exception) {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    CustomError error = new CustomError(exception.getMessage());
    return new ResponseEntity<CustomError>(error,headers, HttpStatus.INTERNAL_SERVER_ERROR);
  }

Any idea how can I make this work or ignore Accept header on response? It´s possible?

Thanks in advance

2
  • Have you solved this problem? I'm also interested in the solution. Commented Sep 8, 2020 at 18:43
  • 1
    @dvelopp nope would love to but no. Commented Dec 7, 2021 at 14:57

1 Answer 1

1

This exception means your response type not match with your request header. If you are expecting JSON/Stream to be returned, your request header should be {"Accept":"application/octet-stream,application/json"}.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.