Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC catch http errors (400.404, ....)

Good afternoon. For several days struggling over the issue. I would like to help with Spring beans (resolver) to catch all the errors in the application. Catching exceptions made almost immediately, but with capture http error is not handled.

The essence of the problem resolver can not intercept the http error.

I do not want to use the web.xml and controller, because I hope that the decision is still using the Spring context.

Implementation of catch exceptions:

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <map>
            ...
            <entry key="java.lang.Throwable" value=".error" />
        </map>
    </property>
    <property name="defaultErrorView" value=".error"/>
</bean>
like image 546
user1573576 Avatar asked Jul 13 '26 21:07

user1573576


1 Answers

I set up mappings for the 40x errors in web.xml then handle them in a controller (which extends SimpleMappingExceptionResolver and handles the 500 ones as well)

<error-page>
    <error-code>404</error-code>
    <location>/404</location>
</error-page>

@RequestMapping(value = "/404")
public String handle404(final HttpServletRequest request, final Model model) {

     final String originalUri = (String)       
        request.getAttribute("javax.servlet.forward.request_uri");
     // etc.
    return "404";
}

I've got a question about the same thing here

like image 183
blank Avatar answered Jul 15 '26 11:07

blank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!