Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3 HandlerInterceptor passing information to Controller

Tags:

java

spring

I've setup a Spring HandlerInterceptor to add an attribute to the HttpServletRequest to be able to read it from the Controller, sadly this does not seem to work which seems strange to me. Am I doing things wrong? Any idea how to transmit the data from the Interceptor to the Controller?

Here is the simplified code of the two impacted classes

public class RequestInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        request.setAttribute("my-role", "superman");
    }
   [...]
}

@RestController
@RequestMapping("Test")
public class TestController {
    public final Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping(value = "something")
    public void something(HttpServletRequest request) {
        logger.info(request.getAttribute("my-role"));
    }

    [...]
}

The request.getAttribute("my-role") returns null... but does return the excepted value if I read it in the postHandle of the HandlerInterceptor, I feel like I'm missing something...

EDIT : I found out that going thru the session with "request.getSession().setAttribute" works as a charm, still i do not understand why the request itself does not work in this use case.

like image 622
JavaCupiX Avatar asked Sep 21 '16 13:09

JavaCupiX


People also ask

How do you intercept HTTP requests in spring boot?

To work with interceptor, you need to create @Component class that supports it and it should implement the HandlerInterceptor interface. preHandle() method − This is used to perform operations before sending the request to the controller. This method should return true to return the response to the client.

How do I use Handlerinterceptoradapter in spring boot?

The HandlerInterceptor contains three main methods: prehandle() – called before the execution of the actual handler. postHandle() – called after the handler is executed. afterCompletion() – called after the complete request is finished and the view is generated.

What is HandlerInterceptor in spring?

Spring HandlerInterceptor declares three methods based on where we want to intercept the HTTP request. boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler): This method is used to intercept the request before it's handed over to the handler method.

How do you get the PATH variable in interceptor spring boot?

Add a Interceptor. Register the Interceptor. With this you will be able to read the @PathVariable by the name you have given to the pathvariable for all the request made. Save this answer.


1 Answers

Can you try with session instead of request like below.

  public boolean preHandle(HttpServletRequest request,
                HttpServletResponse response, Object handler) throws Exception {
                ...
                HttpSession session = request.getSession();
                session.setAttribute("attributeName", objectYouWantToPassToHandler);
                ....
                }
    In your handler handleRequest method:

       public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {                

            ....
            HttpSession session = request.getSession();
            objectYouWantToPassToHandler objectYouWantToPassToHandler = session.getAttribute("attributeName");
            ....


  }
like image 178
Pradeep Avatar answered Oct 16 '22 23:10

Pradeep