Using Spring, I have a SimpleMappingExceptionResolver that catches any unexpected exceptions in my application in the resolveException method. In the method, I return a ModelAndView that returns the text of the error message to the HTTP client. Here is the code:
public class UnExpectedExceptionResolver extends SimpleMappingExceptionResolver { private Log logger = LogFactory.getLog(this.getClass().getName()); private ResourceBundleMessageSource messageSource; @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) {
Thus, the response is returned with an HTTP status code of 200 with the response text being a message (JSON). Unfortunately, the client considers it a valid answer due to code 200 and is trying to process it as such. I tried setting the HTTP status code to 500 as follows:
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Server Error");
front
return mav;
statement. Unfortunately, this returns an HTML page indicating an internal error instead of my JSON message. How can I return a JSON message and still indicate a server error (or some error) for the client? In particular, I expect the client error function in the AJAX call to be called and still be sent back to the message. FYI - I am using jQuery on the client side.
James source share