Wayne Werner
Wayne Werner

Reputation: 51897

Why doesn't this Java servlet url-pattern/RequestMapping work?

I have this RequestMapping/method defined:

@RequestMapping( value={"/ViewReport/json", "/ViewReport/*/json"} , method = RequestMethod.GET)
public ModelAndView TestJson(final Model model, HttpServletRequest request){
    JSONObject json = new JSONObject();
    json.put("Hello", "Goodbye");
    json.put("request", request.getRequestURL());
    model.addAttribute("fnord", json.toJSONString());   
    return new ModelAndView("reportViewJson");  
}    

In my web.xml file I have the following:

  <servlet-mapping>
    <servlet-name>autoreport</servlet-name>
    <url-pattern>/ViewReport/*/json</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>autoreport</servlet-name>
    <url-pattern>/ViewReport/json</url-pattern>
  </servlet-mapping>

Now, when I navigate to /ViewReport/json I get back the expected JSON. However, when I navigate to /ViewReport/42/json I get a 404.

I've got the following logs from when my server starts up:

[ INFO] 23:51(AbstractUrlHandlerMapping.java:registerHandler:315)
Mapped URL path [/ViewReport/] onto handler 'reportViewController'

[DEBUG] 23:51(AbstractBeanFactory.java:doGetBean:246)
Returning cached instance of singleton bean 'reportViewController'

[ INFO] 23:51(AbstractUrlHandlerMapping.java:registerHandler:315)
Mapped URL path [/ViewReport/json] onto handler 'reportViewController'

[DEBUG] 23:51(AbstractBeanFactory.java:doGetBean:246)
Returning cached instance of singleton bean 'reportViewController'

[ INFO] 23:51(AbstractUrlHandlerMapping.java:registerHandler:315)
Mapped URL path [/ViewReport/json.*] onto handler 'reportViewController'

[DEBUG] 23:51(AbstractBeanFactory.java:doGetBean:246)
Returning cached instance of singleton bean 'reportViewController'

[ INFO] 23:51(AbstractUrlHandlerMapping.java:registerHandler:315)
Mapped URL path [/ViewReport/json/] onto handler 'reportViewController'

[DEBUG] 23:51(AbstractBeanFactory.java:doGetBean:246)
Returning cached instance of singleton bean 'reportViewController'

[ INFO] 23:51(AbstractUrlHandlerMapping.java:registerHandler:315)
Mapped URL path [/ViewReport/*/json] onto handler 'reportViewController'

[DEBUG] 23:51(AbstractBeanFactory.java:doGetBean:246)
Returning cached instance of singleton bean 'reportViewController'

[ INFO] 23:51(AbstractUrlHandlerMapping.java:registerHandler:315)
Mapped URL path [/ViewReport/*/json.*] onto handler 'reportViewController'

[DEBUG] 23:51(AbstractBeanFactory.java:doGetBean:246)
Returning cached instance of singleton bean 'reportViewController'

[ INFO] 23:51(AbstractUrlHandlerMapping.java:registerHandler:315)
Mapped URL path [/ViewReport/*/json/] onto handler 'reportViewController'

Update 2

In my autoreport-servlet.xml I've got this:

<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property
        name="webBindingInitializer">
        <!-- Configures Spring MVC DataBinder instances -->
        <bean
            class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="validator" ref="validator" />
        </bean>
    </property>
</bean>

Upvotes: 1

Views: 4114

Answers (2)

Fabian Barney
Fabian Barney

Reputation: 14559

The following mapping is invalid due to Servlet Specification (Section 11):

<servlet-mapping>
    <servlet-name>autoreport</servlet-name>
    <url-pattern>/ViewReport/*/json</url-pattern>
</servlet-mapping>

A *is not allowed in between a path mapping. For path mappings it is only allowed at the end with a /*. (Well, when nitpicking it is in fact valid and allowed but it isn't treated as a wildcard as intended.)

Cited from the Servlet Specifiaction:

In theWeb application deployment descriptor, the following syntax is used to define mappings:

  • A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.
  • A string beginning with a ‘*.’ prefix is used as an extension mapping.
  • A string containing only the ’/’ character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
  • All other strings are used for exact matches only.

So your pattern /ViewReport/*/json is an exact match pattern with no wildcard. That's the reason why /ViewReport/42/json gives a HTTP 404, because it does not match any pattern.

Upvotes: 2

kgautron
kgautron

Reputation: 8283

Because when using spring MVC you don't configure the mapping in the web.xml for each controller method, that's what the @RequestMapping annotation is for.

You configure the spring dispatcher servlet once for all to a URL, then the @RequestMapping annotations handle the mapping, relatively to the spring dispatcher servlet URL.

For example is spring dispatcher servlet is mapped to /mvc/*, and you declare a method with @RequestMapping("/xyz"), the URL for this method will be <contextPath>/mvc/xyz. If you configured spring dispatcher servlet to /* the URL will be <contextPath>/xyz.

Upvotes: 0

Related Questions