3

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>

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

The solution of this occurred. But really no standard solutions Spring?
0

One way is to use HandlerExceptionResolver interface.

An alternative to the HandlerExceptionResolver interface is the @ExceptionHandler annotation. You use the @ExceptionHandler method annotation within a controller to specify which method is invoked when an exception of a specific type is thrown during the execution of controller methods. For example:

package com.spring3;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {

    @ExceptionHandler(Exception.class)
    public ModelAndView handleMyException(Exception exception) { 
        ModelAndView mv = new ModelAndView("redirect:errorMessage.html?error=" + exception.getMessage());
        return mv; 
    }

    @RequestMapping(value = "/errorMessage", method = RequestMethod.GET)
    public ModelAndView handleMyExceptionOnRedirect(@RequestParam("error") String error) {
        ModelAndView mv = new ModelAndView("uncaughtExceptionSpring"); 
        v.addObject("error", error);
        return mv; 
    }

    @RequestMapping("/hello")
    public ModelAndView helloWorld() throws Exception {
        String message = "Hello World, Spring 3.0!";
        return new ModelAndView("hello", "message", message);
    }
}

Spring MVC exception handling and show custom view Part1, 2, 3, 4

2 Comments

In fact of the matter is that you do not want to use the controller.
This works for handling exceptions, however, the question is about handling HTTP errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.