Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3.1 How do you send all exception to one page?

I have the following

web.xml

<error-page>
    <error-code>500</error-code>
    <location>/WEB-INF/views/errorPages/500.jsp</location>
</error-page>

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/WEB-INF/views/errorPages/error.jsp</location>
</error-page>

<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/WEB-INF/views/errorPages/500.jsp</location>
</error-page>

spring-context.xml

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
    <props>
        <prop key="java.lang.Exception">error</prop>
    </props>
    </property>
</bean>

<bean id="exceptionResolver"
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

    <property name="exceptionMappings">
        <props>
            <prop key="com.company.server.exception.GenericException">GenericExceptionPage</prop>
            <prop key="java.lang.Exception">error</prop>
        </props>
    </property>

    <property name="defaultErrorView" value="defaulterror"></property>
</bean>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean> 

GenericException.java

public class GenericException extends RuntimeException
{
  private static final long serialVersionUID = 1L;

  private String customMsg;
  public String message;

  public String getCustomMsg()
  {
    return customMsg;
  }

  public void setCustomMsg(String customMsg)
  {
    this.customMsg = customMsg;
  }

  public GenericException(String customMsg)
  {
    this.customMsg = customMsg;
  }

  @Override
  public String getMessage()
  {
    return message;
  }

}

myController.java

@Controller
@RequestMapping(value = "/admin/store")
public class AdminController
{


 //bunch of restful drivin requestMappings


}

Question: How do I get any and all internal server errors & exceptions to display the exception/error message to a single page?

like image 600
stackoverflow Avatar asked Dec 08 '22 20:12

stackoverflow


1 Answers

I would consider using the @ExceptionHandler annotation on a method in my controller. This annotation marks a method to be invoked by Spring when an Exception bubbles its way up past the controller.

This way, when one of your @RequestMapping methods throws an Exception, then this method will be invoked and can return whatever error message you would like.

public class BaseController {
    @ExceptionHandler(Throwable.class)
    public String handleException(Throwable t) {
        return "redirect:/errorPages/500.jsp";
    }

    @ExceptionHandler(Exception.class)
    public String handleException(Throwable t) {
        return "redirect:/errorPages/error.jsp";
    }
}

@Controller
@RequestMapping(value = "/admin/store")
public class AdminController extends BaseController
{
    @RequestMapping(...)
    //Some method

    @RequestMapping(...)
    //Another method
}
like image 104
nicholas.hauschild Avatar answered Dec 27 '22 01:12

nicholas.hauschild