【问题标题】:Spring MVC - No mapping found for request URISpring MVC - 找不到请求 URI 的映射
【发布时间】:2011-01-14 23:59:28
【问题描述】:

在 StackOverflow 上有很多与此错误相关的问题,我已经尝试了最相关问题的解决方案,但均未成功。这是我的问题。

我正在尝试映射此请求:/user/{userId} 其中userId 是一个字符串。我可以使用以下带注释的类和Spring 配置处理对/user 的GET 请求:

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

我有可怕的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'

我做错了什么?

【问题讨论】:

    标签: java spring spring-mvc controller


    【解决方案1】:

    您通常不会将调度程序 servlet 映射到的实际 servlet 路径包含在请求映射中。请求映射是相对于调度程序的。在退化的第一种情况下,调度程序足够聪明,可以弄清楚你的意思,但是当你开始添加路径变量时,它就会崩溃。您应该能够访问 /user/user/foo 并使用您当前的设置获得所需的内容。

    【讨论】:

    • 这解决了!我不清楚这是如何工作的:请求 /user 和 /user/user 都会导致 userId=user。为什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 2013-12-20
    • 1970-01-01
    相关资源
    最近更新 更多