In a web controller I have an @ExceptionHandler implementation to handle a certain type of device exception I can get. It looks like this :
@ExceptionHandler(DeviceException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public void handleException(DeviceException ex) {
log.error("controller caught exception " + ex.getMessage());
return ex.getMessage();
}
What I want to achieve is to have this return the Exception's message when a DeviceException is thrown.
On the client side I have this jQuery code, current problem is I can't seem to get any of the ex.getMessage() content in the error callback, that's what I need to solve.
$.ajax( {
type: "GET",
url: "<c:url value="/x/y/status"/>.json",
dataType: "json",
cache: false,
success: function ( data ){
// use the data
},
error: function(jqXHR, textStatus, errorThrown) {
// problem : all 3 of the data below (responseText etc.) are b开发者_JAVA技巧lank when I've thrown a DeviceException
alert('jqXHR.responseText = ', jqXHR.responseText);
alert('textStatus = ', textStatus);
alert('errorThrown = ', errorThrown);
}
} );
@ResponseStatus has a value field too. This can be used to set any custom messages. Check the documentation @ http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/ResponseStatus.html
The @ExceptionHandler
mechanism is very flexible with the parameters and return types the method annotated with it can have.
You can use some combination of the HttpServletResponse
object or returning a Model and/or View to return something meaningful to the AJAX caller - for example, a specific response code, or write something to the output stream you can parse in JavaScript, etc.
精彩评论