1

I'm using Spring's @ExceptionHandler annotation to catch exceptions in my controllers.

When there are no arguments in the signature of the method, it's working fine (the method is called), e.g.

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

My goal is to do a redirect when the exception is caught. But when I add some argument, e.g. 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();
    }
}

I get following exception, when the method is called:

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)
    ...

According to API, HttpServletResponse is a valid argument. I've tried to google it, but there are not many posts about Unsupported argument exception concerning ExceptionHandler. Does somebody have any idea why it's working without arguments, but with arguments it's not? Any help is greatly appreciated.

I'm using Spring 3.2.0.

EDIT

Thanks, M. Deinum, I haven't realized I can't use HttpServletRequest. Now, when I've changed it to ActionResponse (I believe that's the one I'm supposed to use, when I want to do a redirect):

@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();
    }
}

I'm getting following exception:

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)
    ...

What am I doing wrong now?

pedron4
  • 11
  • 3
  • 2
    You are in a portlet environment NOT a servlet environment. Hence you have a `PortletRequest` not a `HttpServletRequest`. – M. Deinum Jul 14 '14 at 13:33

0 Answers0