简体   繁体   中英

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. Here is my 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:

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:

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. RememberNameController directly extends 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. 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/

I think the problem is that you are mixing what is intended to be a Spring Controller with plain Servlets. Spring Controllers are not Servlets, they are regular POJOs that are used by Spring MVC for handling the requests (called by the Dispatcher Servlet)

If you want to see a modern way of creating a REST Service using Spring 4, check out this guide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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