Exceptionhandling

Spring-Json View provides an HandlerExceptionResolver (JsonExceptionResolver) to catch any Exception thrown during a controller action.

  1. Configuration
  2. JsonErrorHandler
  3. JsonExceptionHandler
    1. ExceptionMessageExceptionHandler
    2. StackTraceExceptionHandler
    3. CustomImplementaton of JsonExceptionHandler

1. Configuration : Spring ApplicationContext

You have to register at least one JsonErrorHandlers or JsonExceptionHandler to manipulate the response in the case of an thrown exception.

Attention
Register the JsonExceptionResolver in the applicationContext.xml.

NOT in the view.xml !!! It would not be found.

JsonErrorHandlers

JsonErrorHandlers signals to the client that something went wrong. For further informations see Errorhandling

  • HttpStatusError

    Sets a new Response-Status by response.setStatus(errorCode).

  • ModelFlagError

    Adds a simple key-value pair to the model.

JsonExceptionHandler

JsonExceptionHandler converts the java.lang.Exception object to be added to to the Json string .

  • ExceptionMessageExceptionHandler

    Adds the java.lang.Exception Message to the model.

    The default modelKey is "exception.message". The ExceptionMessageExceptionHandler adds the ErrorMessage in the "Exception Classname : Exception Message" format.

    Example : "java.lang.IllegalArgumentException : Please set Parameter"

  • StackTraceExceptionHandler

    Adds the whole Exception StackTrace to the model.

    The default modelKey is "exception.stacktrace". You can replace the linebreak "\n" with an html-linebreake </br> by setting the replaceLineBreakes property true.

    The default value is replaceLineBreakes=false.

<beans>
   <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="exceptionMessageExceptionHandler" />
                        <ref bean="stackTraceExceptionHandler" />
                </list>
        </property>
        </bean>
        
        <bean name="exceptionMessageExceptionHandler" 
              class="org.springframework.web.servlet.view.json.exception.ExceptionMessageExceptionHandler" />
        <bean name="stackTraceExceptionHandler" 
              class="org.springframework.web.servlet.view.json.exception.StackTraceExceptionHandler" />
        
        <bean name="statusError" 
              class="org.springframework.web.servlet.view.json.error.HttpStatusError"/>
        <bean name="modelFlagError" 
              class="org.springframework.web.servlet.view.json.error.ModelFlagError"/>
        
</beans>

Result:
=======

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.[...]
                        [...]"
}

2. ExceptionMessageExceptionHandler

You can customize the model-key of the ExceptionMessage. Default is exception.message

<beans>
   <bean id="exceptionResolver" 
         class="org.springframework.web.servlet.view.json.exception.JsonExceptionResolver">
        
        <property name="exceptionView"><value>jsonView</value></property>
                <property name="exceptionHandler">
                <list>
                        <ref bean="exceptionMessageExceptionHandler" />
                </list>
        </property>
        </bean>
        
        <bean name="exceptionMessageExceptionHandler" 
              class="org.springframework.web.servlet.view.json.exception.ExceptionMessageExceptionHandler">
              <property name="modelKey"><value>myKey</value></property>
        </bean>
        
</beans>

Result:
=======

Response-Status : 200

{
        "myKey":"java.lang.Exception: You throw an exeption !"
}

3. StackTraceExceptionHandler

You can customize the model-key of the Exception-StackTrace (Default is exception.stacktrace); You also can prepare the StackTrace for an html-view by replacing the linebreak "\n" with an html-linebreake </br>. The default value is replaceLineBreakes=false.

<beans>
   <bean id="exceptionResolver" 
         class="org.springframework.web.servlet.view.json.exception.JsonExceptionResolver">
        
        <property name="exceptionView"><value>jsonView</value></property>
        <property name="exceptionHandler">
                <list>
                        <ref bean="stackTraceExceptionHandler" />
                </list>
        </property>
        </bean>
        
        <bean name="stackTraceExceptionHandler" 
              class="org.springframework.web.servlet.view.json.exception.StackTraceExceptionHandler">
              
              <property name="replaceLineBreakes"><value>true</value></property>
              <property name="modelKey"><value>myKey</value></property>
              
        </bean>
        
</beans>

Result:
=======

Response-Status : 200

{
        "myKey": "java.lang.Exception: You throw an exeption !
                <\br>\tat org.thing.spring.json.controller.[...]
                <\br>\tat org.springframework.web.servlet.mvc.[...]
                [...]"
}

4. Custom implementation of JsonExceptionHandler

It is possible to realize some totally different behaviours by implementing the JsonExceptionHandler interface.

public class MyExceptionHandler implements JsonExceptionHandler {
    public void triggerException(Exception exception, Map model, 
                                 HttpServletRequest request, HttpServletResponse response) 
                                 throws Exception{
                        
                        // Do something ...
    }
        
}