The exceptionhandling is realized by an implemention of an HandlerExceptionResolver: The JsonExceptionResolver. You have to register at least one JsonErrorHandlers or JsonExceptionHandler to manipulate the response in the case of an thrown exception.
JsonErrorHandlers signals the client that something went wrong. For further informations see Errorhandling
JsonExceptionHandler convert the java.lang.Exception object to be added to to the json-string.
For further information see the Exceptionhandling
<beans> <bean name="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver" /> <bean name="throwExceptionGetController" class="org.thing.spring.json.controller.ThrowExceptionGetController"/> <bean id="exceptionResolver" class="org.springframework.web.servlet.view.json.exception.JsonExceptionResolver"> <property name="exceptionView"><value>jsonView</value></property> <property name="errorHandler"> <list> <ref bean="statusError" /> <ref bean="modelflagError" /> </list> </property> <property name="exceptionHandler"> <list> <ref bean="stackTraceExceptionHandler" /> <ref bean="exceptionMessageExceptionHandler" /> </list> </property> </bean> <bean name="statusError" class="org.springframework.web.servlet.view.json.error.HttpStatusError"/> <bean name="modelflagError" class="org.springframework.web.servlet.view.json.error.ModelFlagError"/ <bean name="stackTraceExceptionHandler" class="org.springframework.web.servlet.view.json.exception.StackTraceExceptionHandler"/> <bean name="exceptionMessageExceptionHandler" class="org.springframework.web.servlet.view.json.exception.ExceptionMessageExceptionHandler"/> </beans>
<beans> <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"> <property name="jsonErrors"> <list> <ref bean="statusError" /> <ref bean="modelflagError" /> </list> </property> </bean> <bean name="statusError" class="org.springframework.web.servlet.view.json.error.HttpStatusError"> <property name="errorCode"><value>311</value></property> </bean> <bean name="modelflagError" class="org.springframework.web.servlet.view.json.error.ModelFlagError"> <property name="name"><value>failure</value></property> <property name="value"><value>true</value></property> </bean> </beans>
<head> <title> First Test Spring Json Demo </title> <script type="text/javascript" src="script/prototype.js"></script> <script type="text/javascript" src="script/behaviour.js"></script> <script type="text/javascript" src="script/behaviour-roles.js"></script> <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"/> </head> </head> <body> <h1>Spring JSON DEMO</h1> <h2>Exception-Handling by JsonExceptionResolver</h2> <button id="throwException">throw exception</button> </body>
var printResult = function(transport){ var result = "Status : " + transport.status + "\n" + "\n" + "Json-Result:" + "\n" + transport.responseText; alert(result); }; var myrules = { 'button#throwException' : function(element){ element.onclick = function(){ new Ajax.Request('exception.json', { method:'get', parameters: {throwException: 'true'}, onFailure: function(transport){ var json = transport.responseText.evalJSON(); printResult(transport); } }); } } }; Behaviour.register(myrules);
public class ThrowExceptionGetController implements Controller { /** Logger for this class and subclasses */ protected final Log logger = LogFactory.getLog(getClass()); public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { if(request.getParameter("throwException")!= null) throw new Exception("You throw an exeption !"); Map model = new HashMap(); model.put("exception", "false"); return new ModelAndView("json1", model); } }
Response-Status : 311 { "failure":"true", "exception.message":"java.lang.Exception: You throw an exeption !", "exception.stacktrace": "java.lang.Exception: You throw an exeption ! \n\tat org.thing.spring.json.controller.[...] \n\tat org.springframework.web.servlet.mvc.[...] [...]" }