Suppose I have a controller that serves GET
request and returns bean to be serialized to JSON and also provides an exception handler for IllegalArgumentEx
I think removing the produces = MediaType.APPLICATION_JSON_VALUE
from @RequestMapping
of the getMetaInformation
will give you the desired result.
The response-type will be negotiated according to the content-type value in the Accept header.
edit
As this does not cover scenario 3,4 here is a solution working with ResponseEntity.class
directly:
@ExceptionHandler(Exception.class)
public ResponseEntity handleIllegalArgumentException(Exception ex) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
return new ResponseEntity(ex.getMessage(), headers, HttpStatus.BAD_REQUEST);
}