Spring MVC - 未找到请求 URI 的映射
StackOverflow 上有很多与此错误相关的问题,我已经尝试了最相关问题的解决方案,但没有成功。这是我的问题。
我正在尝试映射此请求:/user/{userId}
,其中userId
是一个字符串。我能够使用以下带注释的类和 Spring< 处理对 /user
的 GET 请求/a> 配置:
UserController.java
@Controller
@RequestMapping("/user")
public class UserController {
private static final Logger log = Logger.getLogger(UserController.class.getName());
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody String info() {
log.debug("mapping succeeded!");
return "<H1>foo</H1>";
}
}
web/WEB-INF/user-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.example"/>
</beans>
web.xml
<servlet>
<servlet-name>user</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>user</servlet-name>
<url-pattern>/user/*</url-pattern>
</servlet-mapping>
然后当我请求 /user
2011-01-14 15:47:41,942 DEBUG [com.example.rest.UserController] (http-11080-1) mapping succeeded!
现在做一些有趣的事情。我将代码更改为以下内容:
@Controller
@RequestMapping("/user")
public class UserController {
private static final Logger log = Logger.getLogger(UserController.class.getName());
@RequestMapping(value="/{userId}", method=RequestMethod.GET)
public @ResponseBody String info(@PathVariable String userId) {
log.debug("mapping succeeded! userId=" + userId);
return "<H1>foo</H1>";
}
}
我有可怕的 未找到映射...
(main) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@36598d00: defining beans [userController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
(main) instantiating UserController
(main) Mapped URL path [/user/*] onto handler 'userController'
(main) Mapped URL path [/user/*.*] onto handler 'userController'
(main) Mapped URL path [/user/*/] onto handler 'userController'
...
(http-11080-1) No mapping found for HTTP request with URI [/user] in DispatcherServlet with name 'user'
(http-11080-1) No mapping found for HTTP request with URI [/user/foo] in DispatcherServlet with name 'user'
(http-11080-1) No mapping found for HTTP request with URI [/user/] in DispatcherServlet with name 'user'
我做错了什么?
There are many questions relating to this error on Stack Overflow, and I've tried the solutions to the most pertinent ones without success. Here is my problem.
I am trying to map this request: /user/{userId}
where userId
is a String. I am able to handle GET requests to /user
with the following annotated class and Spring configuration:
UserController.java
@Controller
@RequestMapping("/user")
public class UserController {
private static final Logger log = Logger.getLogger(UserController.class.getName());
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody String info() {
log.debug("mapping succeeded!");
return "<H1>foo</H1>";
}
}
web/WEB-INF/user-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.example"/>
</beans>
web.xml
<servlet>
<servlet-name>user</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>user</servlet-name>
<url-pattern>/user/*</url-pattern>
</servlet-mapping>
Then when I request /user
2011-01-14 15:47:41,942 DEBUG [com.example.rest.UserController] (http-11080-1) mapping succeeded!
Now to do something interesting. I change my code to the following:
@Controller
@RequestMapping("/user")
public class UserController {
private static final Logger log = Logger.getLogger(UserController.class.getName());
@RequestMapping(value="/{userId}", method=RequestMethod.GET)
public @ResponseBody String info(@PathVariable String userId) {
log.debug("mapping succeeded! userId=" + userId);
return "<H1>foo</H1>";
}
}
I have the dreaded No mapping found...
(main) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@36598d00: defining beans [userController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
(main) instantiating UserController
(main) Mapped URL path [/user/*] onto handler 'userController'
(main) Mapped URL path [/user/*.*] onto handler 'userController'
(main) Mapped URL path [/user/*/] onto handler 'userController'
...
(http-11080-1) No mapping found for HTTP request with URI [/user] in DispatcherServlet with name 'user'
(http-11080-1) No mapping found for HTTP request with URI [/user/foo] in DispatcherServlet with name 'user'
(http-11080-1) No mapping found for HTTP request with URI [/user/] in DispatcherServlet with name 'user'
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您通常不会将调度程序 servlet 映射到的实际 servlet 路径包含在请求映射中。请求映射是相对于调度程序的。在退化的第一种情况下,调度程序足够聪明,可以理解您的意思,但是当您开始添加路径变量时,它就会崩溃。您应该能够使用当前的设置访问
/user/user/foo
并获取您要查找的内容。You don't usually include the actual servlet path that your dispatcher servlet is mapped to as part of your request mapping. The request mapping is relative to the dispatcher. In the degenerate first case the dispatcher is smart enough to figure out what you meant, but when you start adding path variables that breaks down. You should be able to access
/user/user/foo
and get what you're looking for, with your current setup.