RequestMappingInfo.getMatchingCondition throwing IllegalArgumentException after upgrading to springboot 2.4.13

Viewed 611

I have a piece of code as below,

final Entry<RequestMappingInfo, HandlerMethod> e = mappingHandler.getHandlerMethods()
        .entrySet()
        .stream()
        .filter(entry -> entry.getKey().getMatchingCondition(request) != null)
        .findFirst()
        .orElse(null);

which is working fine in spring-boot version 2.3.8.RELEASE but if I upgrade it to 2.4.13 it throws an exception as below,

java.lang.IllegalArgumentException: Expected lookupPath in request attribute "org.springframework.web.util.UrlPathHelper.PATH".
at org.springframework.util.Assert.notNull(Assert.java:201)
at org.springframework.web.util.UrlPathHelper.getResolvedLookupPath(UrlPathHelper.java:213)
at org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getMatchingCondition(PatternsRequestCondition.java:280)
at org.springframework.web.servlet.mvc.method.RequestMappingInfo.getMatchingCondition(RequestMappingInfo.java:406)

I tried to look up online and found https://github.com/spring-projects/spring-framework/issues/27715 but I am not able to relate as the issue says something about building mappings yourself which I am not doing at all. I am invoking the Rest Endpoint using a MockMvc object which hits the above code.

1 Answers

Got a workaround for this from SpringFramework issues.

However, I was using the older mechanism of path/request matching in RequestMappingInfo and was using RequestMappingHandlerMapping in Filter specifically OncePerRequestFilter. Certain attributes like PATH are set later in the code hence the error. Though as per community thread it is bad practise to use RequestMappingHandlerMapping (the way I have above)inside OncePerRequestFilter calling below code explicitly resolved the issue for me,

requestMappingHandlerMapping.getUrlPathHelper().resolveAndCacheLookupPath(request);
Related