【问题标题】:Spring MVC controller rejected (No URL paths identified)Spring MVC 控制器被拒绝(未识别 URL 路径)
【发布时间】:2014-05-11 13:41:59
【问题描述】:

我的项目中的 Spring MVC 有一个奇怪的问题,我的 bean 被拒绝并显示以下消息:

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

这是控制器的代码:

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。

谢谢!

【问题讨论】:

标签: spring spring-mvc


【解决方案1】:

尝试从控制器中删除 @RequestMapping("/restful")。请求映射是相对于 servlet url 映射的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2012-06-08
    • 2016-11-11
    相关资源
    最近更新 更多