1
votes

Fresh off my last adventure, now I'm trying to map more complex URLs and going nuts trying to make it work the way Spring's documentation suggests it should.

Again, the tools are:

  • Java 1.6
  • Spring 3.2 MVC
  • Tomcat 7

What I'm trying to do is match URLs of the form foo/bar/id where id is an integer. The way it seems like I should do it is to annotate my controller method like this:

@RequestMapping("/foo/bar/{id}")

And then have this in web.xml:

<url-filter>/foo/*</url-filter>

Or this:

<url-filter>/foo/bar/*</url-filter>

And then after deploying to Tomcat, I should be able to access /mycontext/foo/bar/id. But that doesn't work.

For completeness, here several variations and results:

  • Method mapping: /foo, url-filter: /foo, result: /mycontext/foo works.
  • Method mapping: /foo/*, url-filter: /foo/*, result: successful mapping of method to /foo/* reported at deployment, but accessing /mycontext/foo/bar fails.
  • Method mapping: /foo/*, url-filter: /foo/bar, result: mapped at deployment, but accessing /mycontext/foo/bar fails.
  • Method mapping: /foo/bar, url-filter: /foo/bar, result: /mycontext/foo/bar works.
  • Method mapping: /foo/bar/*, url-filter: /foo/bar/*, result: mapped at deployment, but accessing /mycontext/foo/bar/(anything) fails.
  • Method mapping: /foo/bar/{id}, url-filter: /foo/bar/*, result: mapped at deployment, but accessing /mycontext/foo/bar/(anything) fails.

All of the failures come with error messages from the DispatcherServlet for mycontext that no mapping was found, even though all of them reported success in setting up mapping at deployment time. Since I'm getting the error from the right DispatcherServlet, that suggests my url-filter settings are fine. But the message about successful mapping at deployment references whatever is in the @RequestMapping annotation, so I don't know what to make of Spring first saying it's fine and then later saying it doesn't match.

Is there something I've failed to understand about wildcards here?

1
Sorry, I'm not familiar with the "url-filter" Will you please give more context around it?digitaljoel

1 Answers

1
votes

If you have @RequestMapping("/foo/bar/{id}") this is mapped after combining with the DispatcherServlet's url-pattern match. Consider a case for eg, where the url-pattern is as follows:

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/dispatcher</url-pattern>
</servlet-mapping>

In this case the DispatcherServlet will map the method only if the call from the client is to : /dispatcher/foo/bar/1

So if you want say @RequestMapping to respond to http://<server>/<context>/foo/bar/1 say, just put your url-pattern for DispatcherServlet as / instead:

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>