Java 找不到URI为的HTTP请求的映射
我试图在SpringDispatcherServlet中处理请求 My web.xml具有以下servlet:Java 找不到URI为的HTTP请求的映射,java,spring,servlets,Java,Spring,Servlets,我试图在SpringDispatcherServlet中处理请求 My web.xml具有以下servlet: <servlet> <servlet-name>ApplicationDispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-p
<servlet>
<servlet-name>ApplicationDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringConfig/WebApplication.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ApplicationDispatcher</servlet-name>
<url-pattern>*.json</url-pattern>
<url-pattern>*.do</url-pattern>
<url-pattern>/entities/*</url-pattern>
</servlet-mapping>
我在应用程序日志中看到以下日志:
2016-01-13 16:15:12 INFO RequestMappingHandlerMapping:180 - Mapped "{[/entity/add/entityDetails.do],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.test.EntityController.saveEntityDetails(com.test.vo.EntityCheckpointsVo)
2016-01-13 16:15:12 INFO RequestMappingHandlerMapping:180 - Mapped "{[/entities/list],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.test.EntitiesController.displayAllEntities()
2016-01-13 16:15:12 INFO RequestMappingHandlerMapping:180 - Mapped "{[/entities/add],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.test.EntitiesController.displayNewEntity()
2016-01-13 16:15:12 INFO DispatcherServlet:476 - FrameworkServlet 'ApplicationDispatcher': initialization completed in 950 ms
2016-01-13 16:15:12 WARN PageNotFound:1116 - No mapping found for HTTP request with URI [/TestProject/entities/add] in DispatcherServlet with name 'ApplicationDispatcher'
2016-01-13 16:15:17 WARN PageNotFound:1116 - No mapping found for HTTP request with URI [/TestProject/entities/add] in DispatcherServlet with name 'ApplicationDispatcher'
2016-01-13 16:17:04 WARN PageNotFound:1116 - No mapping found for HTTP request with URI [/TestProject/entities/add] in DispatcherServlet with name 'ApplicationDispatcher'
我没有任何线索,因为日志上说注册了/entities/add
。我可以访问其他URL,如localhost:8080/TestProject/entity/add/entityDetails.do,但我无法访问localhost:8080/TestProject/entities/add
请帮帮我
谢谢
更新:
EntityController中的代码段
@Controller
@RequestMapping(value = "/entity")
public class EntityController {
@RequestMapping(value = "/add/entityDetails.do", method = RequestMethod.GET)
public String saveEntityDetails(EntityCheckpointsVo entityCheckpointsVo) {
return "success";
}
}
我现在可以看到区别了
@Controller(value="entities")
public class EntitiesController {
vs
您在EntitiesController
中没有RequestMapping
。。。您可能知道,@Controller
中的值
是组件的逻辑名称
注意:基于我不确定这是一个错误还是一个设计…如果你尝试点击下面的url,它会起作用
localhost:8080/TestProject/entities/entities/add
这是因为url中的第一个“实体”由于web.xml中的/entities/*模式而被使用。使用此字符串后,剩余的路径uri将转到dispatcher。在这种情况下,entities/add转到dispatcher,它可以正常工作
然而,对于您提到的url,“实体”将被使用,并且只剩下“添加”,而dispatcher没有映射 如果您有如下servlet映射:
<url-pattern>/abc/def/*</url-pattern>
对于url/entities/add请求映射,它将是:
localhost:8080/TestProject/abc/def/entities/add
相关类、方法名称和代码片段,以显示从spring源代码中消耗发生的位置。 我找不到链接。因此,我直接进入了代码。如果您按顺序遍历这些提到的类和方法,您可以看到它使用路径uri的原因: Dispatcher Servlet和相关类的代码片段:
1.org.springframework.web.servlet.DispatcherServletgetHandler(HttpServletRequest请求)
2.org.springframework.web.servlet.handler.AbstractHandlerMappinggetHandler(HttpServletRequest请求)
3.org.springframework.web.servlet.handler.AbstractHandlerMappinggetHandlerExecutionChain(对象处理程序,HttpServletRequest请求) 字符串lookupPath=this.urlPathHelper.getLookupPathForRequest(请求);
4.org.springframework.web.util.UrlPathHelpergetLookupPathForRequest(HttpServletRequest请求)。此.AlwaySuseFilPath=false的默认值为false。发生了来自路径uri的消费。您可以使用变量“rest”,它将包含我们的spring请求映射,如本文中的/entities/add
5.org.springframework.web.util.UrlPathHelpergetPathWithinServletMapping(HttpServletRequest请求)String path=getRemainingPath(pathWithinApp,servletPath,false)
从这里,您可以轻松地深入了解它是如何使用路径uri的。我认为,一旦您在servletMapping中提到/而不是实体,问题就会得到解决。可能是未声明的HTTP方法吗?我在您的日志中看到了“methods=[]”。@Berger在这种情况下,spring会告诉method not supportedI注意,您没有将基
/
用于dispatcher servlet本身的映射,并且您有多种映射模式。为什么,使用Spring Boot并完全跳过web.xml
的复杂过程有可能吗?@Jango:当我提到/as url映射时,/TestProject/index.html
没有被映射。你确定它被使用了吗?和*.do
未被使用?最后一个条目不会像*.docheck此链接中的最后一篇文章那样被使用。试图找到一个更好的链接我只是自己测试了一下,你是对的,它被消耗了。我已经编辑了我的答案。它现在有了类、方法和一些代码片段的名称,以显示这种消耗发生在代码中的什么地方。如果你感兴趣,你可以看看
localhost:8080/TestProject/entities/add
<url-pattern>/abc/def/*</url-pattern>
localhost:8080/TestProject/abc/def/{custom request mapping}
localhost:8080/TestProject/abc/def/entities/add
public String getLookupPathForRequest(HttpServletRequest request) {
// Always use full path within current servlet context?
if (this.alwaysUseFullPath) { // default this.alwaysUseFullPath=false
return getPathWithinApplication(request);
}
// Else, use path within current servlet mapping if applicable
String rest = getPathWithinServletMapping(request);
if (!"".equals(rest)) {
return rest;
}
else {
return getPathWithinApplication(request);
}
}