Search code examples
exceptionspring-roohttp-response-codes

Customizing response code on uncaught exception in Roo


I have a Roo-based application.

If an exception got thrown from my controller, my application will render a stack trace to the end user, but the response will have the status code of 200.

How can I customize this so that any uncaught exception will result in a response with code 500?


Solution

  • In your webmvc-config.xml set defaultStatusCode property on org.springframework.web.servlet.handler.SimpleMappingExceptionResolver.

    Example:

    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" p:defaultErrorView="uncaughtException">
        <property name="exceptionMappings">
            <props>
                <prop key=".DataAccessException">dataAccessFailure</prop>
                <prop key=".NoSuchRequestHandlingMethodException">resourceNotFound</prop>
                <prop key=".TypeMismatchException">resourceNotFound</prop>
                <prop key=".MissingServletRequestParameterException">resourceNotFound</prop>
            </props>
        </property>
        <property name="defaultStatusCode">
            <value>500</value>
        </property>
    </bean>