1

When I go to the first url, it calls my home() method in the controller but when I go to the second url it does not call my homeTest() method. Why is that?

I get 404 error.

http://localhost:9083/MYAPP/foo       ------ first url
http://localhost:9083/MYAPP/foo/bar  ------ second url

web.xml

<servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
        <servlet-name>springServlet</servlet-name>
        <url-pattern>/foo/*</url-pattern>
</servlet-mapping>

Controller:

@RequestMapping(value="/foo", method = RequestMethod.GET)
public String home(Model model){
    return "home";
}

@RequestMapping(value="/foo/bar", method = RequestMethod.GET)
public String homeTest(Model model){
    return "home";
}
5
  • what error do you get? Commented Oct 20, 2016 at 18:56
  • @JohnDonn I get SRVE0295E:Error reported: 404 Commented Oct 20, 2016 at 19:01
  • 1
    You can try to debug Spring's DispatcherServlet (or change your log levels accordingly). At some point you should see, if I recall correctly, a list of all URL paths mapped. This might help you to understand what is going on. Commented Oct 20, 2016 at 19:17
  • Please provide a minimal reproducible example. Commented Oct 20, 2016 at 19:34
  • I have an analogous issue, and have logging turned up. It helps, but I'm still not able to figure out what I'm doing wrong. Commented Mar 14, 2024 at 20:04

2 Answers 2

4

You need to configure your RequestMappingHandlerMapping.

The official documentation goes into detail about handler mappings and some of their properties. The relevant one here is alwaysUseFullPath:

  • alwaysUseFullPath If true, Spring uses the full path within the current Servlet context to find an appropriate handler. If false (the default), the path within the current Servlet mapping is used. For example, if a Servlet is mapped using /testing/* and the alwaysUseFullPath property is set to true, /testing/viewPage.html is used, whereas if the property is set to false, /viewPage.html is used

In short, when trying to find a mapping for /foo/bar, it removes the part that was matched by the Servlet environment, the /foo, and only uses the /bar to find a handler. You have no handler mapped to /bar.

By setting the property above to true, it will use the full path.

You can configure this in a @Configuration annotated WebMvcConfigurationSupport subclass, by overriding requestMappingHandlerMapping

@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
    RequestMappingHandlerMapping handlerMapping = super.requestMappingHandlerMapping();
    handlerMapping.setAlwaysUseFullPath(true);
    return handlerMapping;
}

Or whatever mechanism is appropriate for your configuration (there's an XML equivalent for example).


There's a special case for the exact match of /foo. It's not particularly relevant here.

Sign up to request clarification or add additional context in comments.

Comments

-1

Just change:

<url-pattern>/foo/*</url-pattern>

To

<url-pattern>/foo/**</url-pattern>

9 Comments

I still get 404 with this change
@Nero keep your controller as it is and only update the servlet mapping. Check the updated answer.
I am not allowed to change the url-pattern, it has to stay /foo/*, so that the urls with "/foo/" are only handled by spring servlet
@Nero Then do this /foo/** instead of /foo/*. check the updated answer.
How do you think a Servlet container interprets **? Why do you think so?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.