Find whether URL mapping exists in Spring MVC 3.1 using code in a controller

java, spring-mvc, url

Solution

You can use requestMappingInfo.getPatternsCondition() to check the mapping path; and it has toString method for you to compare with your url string.

Set<RequestMappingInfo> rmSet = handlerMapping.getHandlerMethods().keySet();
for (RequestMappingInfo rm : rmSet) {

    if("[YourURLPath]".equals(rm.getPatternsCondition().toString())) {
        // URL mapping matched 
    }
}

Example: For url `http://mydomain/test/abc` the above condition should be as

`if("[/test/abc]".equals(rm.getPatternsCondition().toString()))`

I believe you have already autowired handlerMapping object in your controller, as below

@Autowired
private RequestMappingHandlerMapping handlerMapping;

Problem

I am using Spring 3.1 MVC and in one of the request I get parameter which is URL. e.g. `http:/myapp/controllername?url=someurl` I need to find out in my controller method whether some URL is configured in my application. I tried using `RequestMappingHandlerMapping` instance to get ``` Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods(); ``` But I have a string which is URL, I will need to create a `RequestMappingInfo` object out of this to look up in this map, which doesn't have any constructor based on just URL. How to find whether an URL mapping exists in spring mvc 3.1 using code in a controller?

Original source