Web Development

Fixing the Spring Web error: Expected lookupPath in request attribute “org.springframework.web.util.UrlPathHelper.PATH”

The other day I was upgrading my Spring Boot app from 2.3.1 to 2.6.6 to address the recent Spring vulnerability. This upgraded the dependent Spring libraries from 5.2.7 to 5.3.18.

At start-up my application dynamically adds some REST endpoints by adding new RequestMappingInfo objects to the RequestMappingHandlerMapping bean already registered in the application context:

ApplicationContext ctx = event.getApplicationContext();

RequestMappingHandlerMapping requestMappingHandlerMapping = ctx.getBean(RequestMappingHandlerMapping.class);

String key = "/v5/person/{id}"
RequestMethod requestMethod = RequestMethod.GET
RequestMappingInfo requestMappingInfo =
                    RequestMappingInfo.paths(key)
                            .methods(requestMethod)
                            .build();

Method method = DynamicPersonController.class.getMethod("handle", HttpHeaders.class, HttpServletRequest.class);
            requestMappingHandlerMapping.registerMapping(requestMappingInfo, handler, method);

When invoking my /v5/person/{id} REST endpoint after the upgrade, this error would be in the console:

Request processing failed; nested exception is java.lang.IllegalArgumentException: Expected lookupPath in request attribute "org.springframework.web.util.UrlPathHelper.PATH"

This problem was caused by changes Spring made to path pattern matching. Spring now wants RequestMappingInfo to use PathPatternsRequestCondition. Previously it used PatternsRequestCondition.

You can opt out of this change after upgrading by setting this property:

spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER

But to use the new preferred PathPatternsRequestCondition and not have to add the property above, I needed to change the configuration of the Builder used to create the RequestMappingInfo.

RequestMappingInfo.BuilderConfiguration options = new RequestMappingInfo.BuilderConfiguration();
options.setPatternParser(new PathPatternParser());

String key = "/v5/person/{id}"
RequestMethod requestMethod = RequestMethod.GET
RequestMappingInfo requestMappingInfo =
                    RequestMappingInfo.paths(key)
                            .methods(requestMethod)
                            .options(options)  //This is Builder config
                            .build();

After adding the RequestMappingInfo.BuilderConfiguration, all my dynamic endpoints started working again.

Published on Java Code Geeks with permission by Steven Wall, partner at our JCG program. See the original article here: Fixing the Spring Web error: Expected lookupPath in request attribute “org.springframework.web.util.UrlPathHelper.PATH”

Opinions expressed by Java Code Geeks contributors are their own.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button