java Spring MVC - 找不到请求 URI 的映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4697167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC - No mapping found for request URI
提问by purecharger
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.
Stack Overflow 上有很多与此错误相关的问题,我已经尝试了最相关的解决方案,但没有成功。这是我的问题。
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 Springconfiguration:
我正在尝试映射此请求:字符串/user/{userId}
在哪里userId
。我能够/user
使用以下带注释的类和Spring配置处理 GET 请求:
UserController.java
用户控制器.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
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
网页.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
然后当我请求 /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...
我有可怕的 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?
我究竟做错了什么?
回答by Affe
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.
您通常不会将调度程序 servlet 映射到的实际 servlet 路径作为请求映射的一部分包含在内。请求映射是相对于调度程序的。在退化的第一种情况下,调度程序足够聪明,可以弄清楚您的意思,但是当您开始添加崩溃的路径变量时。/user/user/foo
使用当前设置,您应该能够访问并获取您要查找的内容。