@ExceptionHandler不支持的参数

时间:2014-07-14 13:29:09

标签: java spring spring-mvc exceptionhandler

我使用Spring的@ExceptionHandler注释来捕获控制器中的异常。

当方法的签名中没有参数时,它工作正常(该方法被称为),例如。

@ExceptionHandler(PortletSessionRequiredException.class)
public void handleExpiredSession() {
    log.error("Session is not available.");
}

我的目标是在捕获异常时执行重定向。但是当我添加一些论点时,例如HttpServletResponse的:

@ExceptionHandler(PortletSessionRequiredException.class)
public void handleExpiredSession(HttpServletResponse response) {
    log.error("Session is not available.");

    try {
        response.sendRedirect("www.example.com");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

当调用该方法时,我得到以下异常:

org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver doResolveException
SEVERE: Invoking request method resulted in exception : public void ...portlet.BaseController.handleExpiredSession(javax.servlet.http.HttpServletResponse)
java.lang.IllegalStateException: Unsupported argument [javax.servlet.http.HttpServletResponse] for @ExceptionHandler method: public void ...portlet.BaseController.handleExpiredSession(javax.servlet.http.HttpServletResponse)
    at org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.resolveHandlerArguments(AnnotationMethodHandlerExceptionResolver.java:243)
    at org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.doResolveException(AnnotationMethodHandlerExceptionResolver.java:115)
    at org.springframework.web.portlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:129)
    at org.springframework.web.portlet.DispatcherPortlet.processHandlerException(DispatcherPortlet.java:1216)
    at org.springframework.web.portlet.DispatcherPortlet.doRenderService(DispatcherPortlet.java:784)
    at org.springframework.web.portlet.FrameworkPortlet.processRequest(FrameworkPortlet.java:523)
    at org.springframework.web.portlet.FrameworkPortlet.doDispatch(FrameworkPortlet.java:471)
    at javax.portlet.GenericPortlet.render(GenericPortlet.java:233)
    at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:103)
    ...

根据API,HttpServletResponse是一个有效的参数。 我试图谷歌它,但没有很多关于ExceptionHandler的不支持的参数异常的帖子。有人知道它为什么没有参数工作,但有参数不是吗?非常感谢任何帮助。

我正在使用Spring 3.2.0。

修改

谢谢,M.Deinum,我还没有意识到我不能使用HttpServletRequest。现在,当我将其更改为ActionResponse时(我相信这是我应该使用的那个,当我想要重定向时):

@ExceptionHandler(PortletSessionRequiredException.class)
public void handleExpiredSession(ActionResponse response) {
    log.error("Session is not available.");

    try {
        response.sendRedirect("www.example.com");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我遇到了以下异常:

org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver doResolveException
SEVERE: Invoking request method resulted in exception : public void ...portlet.BaseController.handleExpiredSession(javax.portlet.ActionResponse)
java.lang.IllegalStateException: Standard argument type [javax.portlet.ActionResponse] resolved to incompatible value of type [class com.liferay.portlet.RenderResponseImpl]. Consider declaring the argument type in a less specific fashion.
    at org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.resolveCommonArgument(AnnotationMethodHandlerExceptionResolver.java:276)
    at org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.resolveHandlerArguments(AnnotationMethodHandlerExceptionResolver.java:238)
    at org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.doResolveException(AnnotationMethodHandlerExceptionResolver.java:115)
    at org.springframework.web.portlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:129)
    at org.springframework.web.portlet.DispatcherPortlet.processHandlerException(DispatcherPortlet.java:1216)
    at org.springframework.web.portlet.DispatcherPortlet.doRenderService(DispatcherPortlet.java:784)
    at org.springframework.web.portlet.FrameworkPortlet.processRequest(FrameworkPortlet.java:523)
    at org.springframework.web.portlet.FrameworkPortlet.doDispatch(FrameworkPortlet.java:471)
    at javax.portlet.GenericPortlet.render(GenericPortlet.java:233)
    ...

我现在做错了什么?

0 个答案:

没有答案