java - 为什么这个 Java servlet url-pattern/RequestMapping 不起作用?

标签 java servlets

我定义了这个RequestMapping/方法:

@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");  
}    

在我的 web.xml 文件中,我有以下内容:

  <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>

现在,当我导航到 /ViewReport/json 时,我得到了预期的 JSON。但是,当我导航到 /ViewReport/42/json 时,我得到了 404。

当我的服务器启动时,我收到以下日志:

[ 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'

更新2

在我的 autoreport-servlet.xml 中,我得到了这个:

<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>

最佳答案

由于 Servlet Specification (Section 11),以下映射无效:

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

路径映射之间不允许有*。对于路径映射,仅允许在末尾带有 /*。 (好吧,当挑剔时,它实际上是有效且允许的,但它没有按预期被视为通配符。)

引用自 Servlet 规范:

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.

所以你的模式 /ViewReport/*/json 是一个没有通配符的精确匹配模式。 这就是 /ViewReport/42/json 给出 HTTP 404 的原因,因为它不匹配任何模式。

关于java - 为什么这个 Java servlet url-pattern/RequestMapping 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20577261/

相关文章:

java - 如何从servlet中的html表中检索数据并将其插入数据库

java - sortable JTable header 的小三角图标是从哪里来的?

java - Java Servlet 中的数据表独立编辑器

java - Google App Engine 用户注册系统

jsp - 从 servlet 调用 jsp

java - 如何使用java代码设置html文本字段的值

maven - 如何通过maven拉取rt.jar?

java - postStop() 方法以防 actor 失败

java - 在 Eclipse 中更改 .classpath 文件的名称和/或路径

java - 我可以在java中保留两个不同版本的相同jar吗?