Spring Boot @ControllerAdvice @ExceptionHandler content type via an annotation

Viewed 76

I know e.g. status can be set via an annotation:

@ExceptionHandler(MyException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String handleMyException(MyException myException) {
    return myException.getMessage();
}

I know I can make a ResponseEntity with the appropriate content type like this:

@ExceptionHandler(MyException.class)
public ResponseEntity<String> handleMyException(MyException myException) {
    return ResponseEntity
        .badRequest()
        .header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
        .body(myException.getMessage());
}

Is it possible to set the content type via annotation only, i.e. something like this:

@ExceptionHandler(MyException.class)
@ContentType(MediaType.TEX_PLAIN_VALUE)
public String handleMyException(MyException myException) {
    return myException.getMessage();
}

I couldn't find an annotation that would do that.

1 Answers
Related