gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:06:45 26 4
gpt4 key购买 nike

我定义了这个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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com