I have a simple webservice that returns content either as json
or as plain text
(depending on the clients' accept
http header).
Problem: if an error occurs during text/plain
request, Spring somehow returns a 406 Not Acceptable
. Which is kind of wrong, because spring could as well just write the error out as plain error text, and moreover should absolutely preserve the 400
error status:
@RestController
public class TestServlet {
@PostMapping(value = "/test", produces = {APPLICATION_JSON_VALUE, TEXT_PLAIN_VALUE, "text/csv"})
public Object post() {
throw new BadRequestException("bad req");
}
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class BadRequestException extends RuntimeException {
public BadRequestException(String msg) {
super(msg);
}
}
POST request with accept=application/json
:
{
"timestamp": "2018-07-30T14:26:02",
"status": 400,
"error": "Bad Request",
"message": "bad req",
"path": "/test"
}
BUT with accept=text/csv
(or text/plain
) shows an empty response with status 406 Not Acceptable
.
I also noticed the DispatcherServlet.processDispatchResult()
is called twice: first with my BadRequest
exception, 2nd time with HttpMediaTypeNotAcceptableException
. So clearly the rendering of my custom exception fails, but why?