Spring MVC控制器被拒绝(未识别URL路径)

时间:2014-05-11 13:42:00

标签: spring spring-mvc

我在一个项目中遇到一个奇怪的问题,就是我的bean被拒绝了,其中包含以下消息:

BeanNameUrlHandlerMapping:86 - 拒绝bean名称'restfulController':未识别URL路径

以下是Controller的代码:

package com.jidarc.mongodb.mvc.controller;

    import org.apache.log4j.Logger;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/restful")
public class RestfulController {

    private static final Logger LOGGER = Logger
            .getLogger(RestfulController.class);

    public RestfulController() {
        LOGGER.debug("INITIALIZING THE RESTFUL CONTROLLER");
    }

    @RequestMapping("/{name}")
    @ResponseBody
    public String get(@PathVariable String name) {
        LOGGER.debug("Received a GET request");

        return "Hello from Spring MVC++++++++++!" + name;
    }
}

web.xml的内容:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Jidarc MongoDB Admin Project</display-name>

    <servlet>
        <servlet-name>restful</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>restful</servlet-name>
        <url-pattern>/restful</url-pattern>
    </servlet-mapping>

</web-app>

restful-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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.jidarc.mongodb.mvc.controller"/>

</beans>

当我尝试联系控制器中定义的URL时,我总是得到404。

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试从Controller中删除@RequestMapping(“/ restful”)。请求映射是相对于servlet url映射的。