2016-05-17 76 views
0

我是新來的Maven的/春天,我在努力弄清楚爲什麼我得到這些錯誤。有一堆這樣的問題在網上,但是嘗試使用可用的解決方案後,我仍然無法解決我的錯誤:org.springframework.web.servlet.PageNotFound noHandlerFound錯誤 - 已經嘗試過的一切,仍然沒有工作:/

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的/你好 - > GET
  2. /helloWorld/displayMessage/{msg} - > GET
  3. /helloWorld/meow - > POST

現在,只有調用1和2工作,而三個是什麼導致上述錯誤。

我使用終端來ping每個端點。以下是我對測試1和測試2所進行的調用的示例:

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

This works。

不過,我得到一個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-調度員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> 

另外,我也行:

有一個*在封裝的結束,因爲我讀一個解決方案,說,加入*固定的問題,卻一直沒有固定的礦山,所以我還沒有拿出來。但它沒有或沒有它工作。

+0

你可以測試你的方法使用郵差,看看你是否得到相同的結果? – Aditya

回答

0

我想你可以在命令錯過-H 'Content-Type:application/json',試試吧;)

+0

沒有工作:(我會繼續嘗試不同的事情,但現在把它放在標題中 – EndingWithAli

相關問題