java - Spring MVC 自定义异常映射不起作用

标签 java spring spring-mvc exception

Web.xml

<error-page>
        <exception-type>org.test.AuthorisationException</exception-type>
        <location>/WEB-INF/jsp/error/unauthorised.jsp</location>
</error-page>

AuthorizationException.java

public class AuthorisationException extends Exception {

    public AuthorisationException(String message) {
        super(message);
    }

    public AuthorisationException(Throwable cause) {
        super(cause);
        // TODO Auto-generated constructor stub
    }

    public AuthorisationException(String message, Throwable cause) {
        super(message, cause);
        // TODO Auto-generated constructor stub
    }
}

servlet.xml

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
      <property name="exceptionMappings">
        <props>
            <prop key="org.test.AuthorisationException">error/unauthorised</prop>
        </props>
      </property>
</bean>

堆栈跟踪

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.Exception: java.lang.reflect.InvocationTargetException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.Exception: java.lang.reflect.InvocationTargetException
    au.com.dash.controller.GatewayController.getGateway(GatewayController.java:106)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.reflect.InvocationTargetException
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    au.com.dash.controller.GatewayController.getGateway(GatewayController.java:72)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

org.test.AuthorisationException: Access Denied!

我认为它不起作用,因为顶级异常org.springframework.web.util.NestedServletException与异常映射不匹配,并且页面未显示。我该如何处理这个问题?

最佳答案

该代码仅处理自定义异常 - AuthorizationException。你想处理异常吗?然后您需要将 java.lang.Exception 映射到错误页面。

检查此示例 - http://www.mkyong.com/spring-mvc/spring-mvc-exception-handling-example/

关于java - Spring MVC 自定义异常映射不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38114298/

相关文章:

java - 使用列表,我如何更改此代码以使用列表

spring - CXF - com.ctc.wstx.exc.WstxUnexpectedCharException : Illegal character ((CTRL-CHAR, 代码 5))

java - Spring ApplicationListener 没有接收到事件

spring - 重写 Spring-Social ConnectController 并添加拦截器

java - 使用 Spring 的 DomainClassConverter 功能进行单元测试

java - 阻碍android应用程序的反编译?

java - 加载图像并在加载完成后进行设置

java - 第二眼 JPA 保存列表导致 PK 违规

spring - 如何在 spring-mvc 中为 springfox(更准确的 springfox-swagger2)设置主机 url?

java - Spring MVC - 对Tomcat中应用程序的所有请求在war打包后导致错误404