I’ve tried this a couple of different ways, including @ExceptionHandler, and using HttpServletResponse as a parameter to the mapped method.
@RequestMapping(value="/update", method=RequestMethod.POST)
public ResponseEntity<String> update(final HttpEntity<String> requestEntity) {
try {
// stuff
return new ResponseEntity<String>("", HttpStatus.NO_CONTENT);
} catch (MyDataException e) {
return new ResponseEntity<String>(e.toJSONString(), HttpStatus.BAD_REQUEST);
}
}
All I want is the contents of toJSONString as the response body. However, with the HttpStatus.BAD_REQUEST value set, the system uses the default Tomcat error page. If I don’t set it to an error status, it creates the body as I intend it. But I must set the error status.
As I said, I’ve tried this about four different ways in the Spring MVC API, and they all end up looking like sendError() is called instead of handling the response in the way that I want.
This was actually a case of PEBKAC, or rather failing to recognize that a poorly written http proxy was actively replacing the response body with a Tomcat error page.