简体   繁体   English

春季4找不到HTTP请求的映射

[英]Spring 4 No mapping found for HTTP request

I'm running Spring 4 and am trying to build a VERY basic REST web service as a proof of concept. 我正在运行Spring 4,并试图构建一个非常基本的REST Web服务作为概念证明。 Here is my web.xml: 这是我的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>

Here's the definition of my servlet in my application context: 这是我的应用程序上下文中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 "> 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 “>

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

Here are the messages I'm getting from Spring: 这是我从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'

Where springpoc is the name of the war file I'm deploying under tomcat. 其中springpoc是我要在tomcat下部署的war文件的名称。 RememberNameController directly extends HttpServlet. RememberNameController直接扩展HttpServlet。

Can someone tell me what I'm doing wrong with my mapping? 有人可以告诉我我的地图出了什么问题吗?

I think this: 我认为这:

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

needs to be this: 需要这样的:

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

Spring controllers should not extend HttpServlet , they should be built with Spring annotations. Spring控制器不应扩展HttpServlet ,而应使用Spring注释构建它们。 eg 例如

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

@Controller
public class HelloWorldController {

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

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

I think the problem is that you are mixing what is intended to be a Spring Controller with plain Servlets. 我认为问题在于您正在将原本打算成为Spring Controller的内容与普通Servlet混合使用。 Spring Controllers are not Servlets, they are regular POJOs that are used by Spring MVC for handling the requests (called by the Dispatcher Servlet) Spring Controller不是Servlet,它们是Spring MVC用于处理请求的常规POJO(由Dispatcher Servlet调用)

If you want to see a modern way of creating a REST Service using Spring 4, check out this guide 如果您想了解使用Spring 4创建REST服务的现代方法,请查看本指南。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM