38

In Spring MVC controller, I can get path variable using @PathVariable to get the value of a variable defined in @RequestMapping. How can I get the value of the variable in an interceptor?

Thank you very much!

Leon
  • 699
  • 2
  • 7
  • 10

4 Answers4

96

The thread linked to by Pao worked a treat for me

In the preHandle() method you can extract the various PathVariables by running the following code

Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); 
ashario
  • 2,694
  • 1
  • 23
  • 19
  • 14
    and then `String value= (String) pathVariables.get("yourPathVarName");` that's it. This should be marked as answer – spiderman Jun 18 '15 at 21:37
  • 1
    Perfect, the example code also works with `@ControllerAdvice` and `@ExceptionHandler`. Thanks – Andreas Apr 08 '16 at 05:55
  • is there a way we can update this path variable in preHandle method? eg: value of "yourPathVarName from" test – TomJava May 28 '20 at 20:59
6

Add a Interceptor.

import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.TreeMap;


@Component
public class MyHandlerInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Map map = new TreeMap<>((Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE));
        Object myPathVariableValue = map.get("myPathVariableName");
        // some code around myPathVariableValue.
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
    }
}

Register the Interceptor.

import com.intercept.MyHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ConditionalOnProperty(name = "mongodb.multitenant.enabled", havingValue = "false")
public class ResourceConfig implements WebMvcConfigurer {

    @Autowired
    MyHandlerInterceptor webServiceTenantInterceptor;
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(webServiceTenantInterceptor);
    }
}

With this you will be able to read the @PathVariable by the name you have given to the pathvariable for all the request made.

Arun Pratap Singh
  • 3,428
  • 30
  • 23
4

There is a thread in the Spring forums, where someone says, there is no "easy way", so i suppose you would have to parse the URL to get it.

Pao
  • 843
  • 5
  • 17
  • 1
    Actually the answer by @ashario above (http://stackoverflow.com/a/23468496/35274) shows that it can be done. – Philippe Jan 17 '17 at 21:13
4

Almost 1 year too late, but:

         String[] requestMappingParams = ((HandlerMethod)handler).getMethodAnnotation(RequestMapping.class).params()

         for (String value : requestMappingParams) {...

should help

hi_my_name_is
  • 4,894
  • 3
  • 34
  • 50
  • 1
    this appears useful for retrieving the RequestParams but I don't see how to get the value of the PathVariables with this approach – chrismarx May 28 '15 at 15:53