I'm trying to extract a request routing data, similar to RouteData object in ASP.NET, to get the controller and action of the request. I'm trying to do it inside a filter, but specific Spring implementation will be sufficient.
Is there any way to get that?
Thanks.
This cannot be done from inside a filter. You have to do it with a HandlerInterceptor
implementation. In the preHandle
method you can get the PathVariables like this:
Map pathVariables = (Map)request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
Here's a short compiled sample:
Implement the HandlerMapping interface:
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
public class ParameterValidationHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
Register the interceptor in the application:
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new ParameterValidationHandlerInterceptor());
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments