Spring 3.1 How do you send all exceptions 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 to get all and all internal server errors and exceptions in order to display the exception / error message on one page?

+4
source share
2 answers

I would like to use the annotation of the @ExceptionHandler method in my controller. This annotation denotes the method that Spring invokes when an Exception bubbles up the way up the controller.

Thus, when one of your @RequestMapping methods throws an Exception , then this method will be called and may return any error message that 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 } 
+11
source

If you want to handle exceptions from all of your controllers, you really should extend SimpleMappingExceptionResolver or, alternatively, AbstractHandlerExceptionResolver and configure it in the container, as indicated here .

This will prevent you (or other employees working in code) from having to subclass all controllers together with one place to eliminate exceptions.

Using annotations and a superclass will work, but the annotation seems to be more focused on using each controller.

In addition, both of these clauses only work for exceptions excluded from controller methods.

If you are worried about exceptions from .jsp files than this post , you should provide an additional solution.

None of the above replace the configuration of the web.xml error page, as their area is not the same. For discussion, this post seems like a good starting point.

+3
source

Source: https://habr.com/ru/post/1437648/


All Articles