Spring 4找不到HTTP请求的映射

时间:2014-03-11 17:45:26

标签: java spring tomcat spring-mvc

我正在运行Spring 4,并且正在尝试构建一个非常基本的REST Web服务作为概念验证。这是我的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 id="poc" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Archetype Created Web Application</display-name>

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

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/ws-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
</web-app>

这是我的应用程序上下文中我的servlet的定义:

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd                         http://www.springframework.org/schema/context                         http://www.springframework.org/schema/context/spring-context-4.0.xsd&#34;&GT;               

<bean name="/rememberName" class="com.corrisoft.air.ws.RememberNameController" init-method="init">
    <property name="personService" ref="personService"/>
</bean>

以下是我从Spring获得的消息:

INFO: Mapped URL path [/rememberName] onto handler '/rememberName'
....
WARNING: No mapping found for HTTP request with URI [/springpoc/ws/rememberName] in DispatcherServlet with name 'ws'

其中springpoc是我在tomcat下部署的war文件的名称。 RememberNameController直接扩展HttpServlet。

有人可以告诉我我的映射错误吗?

3 个答案:

答案 0 :(得分:1)

我想这个:

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

需要这样:

<servlet-mapping>
    <servlet-name>ws</servlet-name>
    <url-pattern>/ws/*</url-pattern>
</servlet-mapping>

答案 1 :(得分:1)

Spring控制器不应该扩展HttpServlet,它们应该使用Spring注释构建。 e.g。

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")
    public String hello() {
        return "helloworld";
    }
}

有关详细信息,请参阅以下教程http://javahash.com/spring-4-mvc-hello-world-tutorial-full-example/

答案 2 :(得分:1)

我认为问题在于您将Spring Controller与普通Servlet混合在一起。 Spring控制器不是Servlet,它们是Spring MVC用于处理请求的常规POJO(由Dispatcher Servlet调用)

如果您想看到使用Spring 4创建REST服务的现代方法,请查看this guide