spring 4找不到http请求的Map

xvw2m8pv  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(256)

我正在运行spring4,并试图构建一个非常基本的restweb服务作为概念证明。这是我的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/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd">

<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。
有人能告诉我我的Map有什么问题吗?

7rtdyuoh

7rtdyuoh1#

我认为:

<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>
dgsult0t

dgsult0t2#

Spring控制器不应伸出 HttpServlet ,它们应该用spring注解构建。例如

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/

q9yhzks0

q9yhzks03#

我认为问题在于,您将spring控制器与普通servlet混合使用。spring控制器不是servlet,它们是springmvc用来处理请求的常规pojo(由dispatcherservlet调用)
如果您想了解使用Spring4创建rest服务的现代方法,请参阅本指南

相关问题