org.springframework.web.servlet.PageNotFound noHandlerFound错误 - 已经尝试了所有内容,仍然无效:/

时间:2016-05-17 23:29:19

标签: java apache rest maven spring-mvc

我是maven / spring的新手,我正在努力弄清楚为什么我会遇到这些错误。网上有很多这样的问题,但是在尝试使用可用的解决方案后,我仍然无法解决我的错误:

May 17, 2016 7:01:47 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/example/helloWorld/meow] in DispatcherServlet with name 'mvc-dispatcher'

我的设置如下:Apache Tomcat( 我正在尝试创建一个REST API,允许我上传和下载文件,以及通过REST调用来回发送消息。我目前定义了3个REST端点:

  1. / helloWorld / hello - > GET
  2. / helloWorld / displayMessage / {msg} - > GET
  3. / helloWorld / meow - > POST
  4. 现在,只有第1和第2个调用工作,而3个是导致上述错误的原因。

    我正在使用终端来ping每个端点。以下是我对测试1和2进行调用的示例:

    curl -X GET localhost:8080/example/helloWorld/hello
    

    这很有效。

    但是,当我执行此命令时,我得到404响应,以及上面的错误:

    curl -X POST localhost:8080/example/helloWorld/meow -d "{'body':1234}"
    

    我很确定我发送的数据是正确的,但还没有达到目标,因为当我检查我的服务器的catalina.out文件时,我看到上面提到的错误,意思是我甚至无法调试我的请求,因为URI不起作用。

    有一个前端定义,但我不关心它,因为我只需要REST调用。

    这是我的控制器:

    ...imports...
    @Controller
    @RequestMapping("/helloWorld")
    public class HelloWorldController {
    
        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        public String hello(ModelMap model) {
            model.addAttribute("msg", "JCG Hello World!");
            System.out.println(new Date().toString() + "testPrint: in hello");
            return "helloWorld";
        }
    
        @RequestMapping(value = "/displayMessage/{msg}", method = RequestMethod.GET)
        public String displayMessage(@PathVariable String msg, ModelMap model) {
            model.addAttribute("msg", msg);
            System.out.println(new Date().toString() + "testPrint: "+msg);
            return "helloWorld";
        }
    
        @RequestMapping(value = "/meow", method = RequestMethod.POST)
        // @ResponseBody
        public ResponseEntity<Boolean> saveData(HttpServletRequest request,
                HttpServletResponse response, Model model) {
            String jsonString = request.getParameter("json");
            System.out.println(new Date().toString() + "testPrint: " + jsonString);
            return null;
        }
    }
    

    这是我的pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.example.example</groupId>
        <artifactId>serverExample</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>springexample Maven Webapp</name>
        <url>http://maven.apache.org</url>
        <dependencies>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>3.0.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.0.1</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
        <build>
            <finalName>example</finalName>
        </build>
    
        <properties>
            <spring.version>4.0.2.RELEASE</spring.version>
        </properties>
    </project>
    

    这是我的web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    
        id="WebApp_ID" version="3.0">
      <display-name>Archetype Created Web Application</display-name>
    
        <servlet>
            <servlet-name>mvc-dispatcher</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>mvc-dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value> /WEB-INF/mvc-dispatcher-servlet.xml </param-value>
        </context-param>
    
       <listener>
        <listener-class>
              org.springframework.web.context.ContextLoaderListener
        </listener-class>
      </listener>
    </web-app>
    

    这是我的mvc-dispatcher-servlet.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
        <context:annotation-config />
        <context:component-scan base-package="com.example.example.thing.*" />
        <mvc:annotation-driven />
    <!--    
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                <value>/WEB-INF/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
        </bean>
     -->
    </beans>
    

    另外,我有这条线:         

    在包的末尾有一个*,因为我读了一个解决方案,说添加*修复了问题,但它没有修复我的,所以我还没有把它拿出来。但无论有没有它都无效。

1 个答案:

答案 0 :(得分:0)

我想你可能会在命令中错过StackInterface,试试吧;)