Search code examples
spring-mvcservletsweb.xmlurl-mapping

In a web.xml, can the url-pattern of servlet-mapping containing more than one path component? (e.g. /path/to/*))


I'm finding the following strange behaviour:

  1. GET http://myserver/myapp/functionality/2.0 works (calls my @Controller method)
2024-03-14 13:59:49,761 [ajp-nio-172.7.102.11-5067-exec-2] INFO  com.mycompany.MyControllerV2 - MyControllerV2 getSomething4() Begin
  1. GET http://myserver/myapp/functionality/2.0/ does not work
2024-03-14 13:59:48,048 [ajp-nio-172.7.102.11-5067-exec-1] WARN  org.springframework.web.servlet.PageNotFound - No mapping for GET /myapp/functionality/2.0/
  1. GET http://myserver/myapp/functionality/2.0/pathvalue does not work

catalina.out

 2024-03-14 13:57:32,953 [main] DEBUG _org.springframework.web.servlet.HandlerMapping.Mappings -
    e.s.a.a.r.c.MyControllerV2:
    {GET [/functionality/2.0/{entity}]}: getSomething2(HttpServletRequest,HttpServletResponse)
    {GET [/functionality/2.0]}: getSomething4(HttpServletRequest,HttpServletResponse)
    {GET [/functionality/2.0/]}: getSomething5(HttpServletRequest,HttpServletResponse)
    {GET [/functionality/2.0/pathvalue]}: getSomething6(HttpServletRequest,HttpServletResponse)
    {GET [/myapp/functionality/2.0/pathvalue]}: getSomething7(HttpServletRequest,HttpServletResponse)
    

web.xml

Note I have a version 1.0 that extends the plain old HttpServlet, hence needing this /2.0/ path component.

        <servlet>
                <servlet-name>NEWAPI</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                <init-param>
                        <param-name>dispatchOptionsRequest</param-name>
                        <param-value>true</param-value>
                </init-param>
                <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
                <servlet-name>NEWAPI</servlet-name>
                <url-pattern>/functionality/2.0/*</url-pattern>
        </servlet-mapping>

NEWAPI-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

        <!--
                - The controllers are autodetected POJOs labeled with the @Controller annotation.
        -->
        <context:component-scan base-package="edu.mycompany" />
</beans>

Tech stack

(Enforced, not my choice)

  • Spring 5,
  • Tomcat 9 and
  • Servlet 2.3,

Notes

  • Apologies if the example is artificial, I needed to erase company secrets.
  • I did read the Servlet specs, but it doesn't seem to address this case

Solution

  • @RequestMapping urls in @Controller must (usually) should be relative to the root url-pattern.

    If the url in the Spring request mapping is the same as the web.xml pattern, it will successfully match as an absolute url. Otherwise, the two components of the url should not overlap.