To globally handle errors (such as HTTP 404's) which can occur outside of a Controller, I have entries similar to the following in my web.xml:
<error-page>
<error-code>404</error-code>
<location>/errors/404</location>
</error-page>
In my ErrorController I have corresponding methods similar to the following:
@Controller
@RequestMapping("/errors")
public class ErrorController {
@RequestMapping(value = "/404", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<ErrorResponse> error404() {
ErrorResponse errorBody = new ErrorResponse(404, "Resource Not Found!");
return new ResponseEntity<ErrorResponse>(errorBody, HttpStatus.NOT_FOUND);
}
}
The issue I'm facing is that the ContentNegotiationManager
and message converters I have configured are not being used in this case. I suspect that since the request is being redirected to the error page, the original request's attributes used in content negotiation are lost and this is treated as a completely separate request. (i.e. original request for /mycontroller/badresource.json --> /errors/404 (w/no file extension))
Is there any way in an error handler like this determine and/or respond with the appropriate content type as requested in the original request?