![](/img/trans.png)
[英]org.springframework.web.servlet.PageNotFound noHandlerFound
[英]org.springframework.web.servlet.PageNotFound noHandlerFound Error - Already tried everything, still not working :/
我是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,該API允許我上傳和下載文件以及通過REST調用來回發送消息。我目前定義了3個REST端點:
目前,只有呼叫1和2有效,而導致上述錯誤的原因是三個。
我正在使用終端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>
另外,我有一行:
軟件包末尾有一個*,因為我讀過一個解決方案,說添加*可以解決問題,但是還沒有解決我的問題,所以我還沒有解決。 但是,無論有沒有它,它都不起作用。
我認為您可能會在命令中錯過-H 'Content-Type:application/json'
,嘗試一下;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.