2016-02-15 109 views
1

我在使用Spring的休息服務器上遇到了一個奇怪的問題。在其他問題上我沒有找到解決我的問題的方法。Spring REST服務「沒有找到具有URI的HTTP請求的映射」

一切工作正常,我有不同的控制器方法的映射,他們都工作。我做了一個新的映射到另一個URI,我不能讓它工作,我總是在嘗試訪問它時遇到404未找到的錯誤。

下面是我的一些代碼:

Restcontroller:

@RequestMapping(value = "/addToken/{token}/imei/{imei}", method = RequestMethod.POST) 
    public boolean setUserToken(@PathVariable("token") String token, @PathVariable("imei") String imei) { 
     boolean result = false; 
     try { 
      result = DbConnector.setToken(imei,token); 
     } catch (Exception e) { 
      System.out.println("Couldn't set token to database"); 
     } 
     return result; 
    } 

這是方法不工作,我不知道爲什麼,當我打電話BASE_URL/addToken我沒有得到任何錯誤,但當我添加任何東西后,我得到404錯誤。

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_2_5.xsd" id="WebApp_ID" version="2.5"> 
<display-name>SpringServiceSample</display-name> 
<servlet> 
    <servlet-name>rest</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>rest</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 
</web-app> 

休息-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<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" 
xmlns:p="http://www.springframework.org/schema/p" 
xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 
<context:component-scan base-package="controllers" /> <!-- Spring auto scans all elements in base package and all its child packages for Controller servlet --> 

<!-- <mvc:annotation-driven> --> 
<!--  <mvc:message-converters> --> 
<!--    <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> --> 
<!--    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> --> 
<!-- </mvc:message-converters> --> 
<!-- </mvc:annotation-driven> --> 

<mvc:annotation-driven /> 
    </beans> 

在此先感謝您的幫助。

+0

這是更好,如果你發佈你打 – Reddy

+0

HTTP網址://本地主機:8080/SpringTest /用戶/ addToken/123/IMEI/123示例(/用戶,因爲我得到了一個更高的控制器的請求映射,但它的工作原理) – Allinone51

+1

setUserToken方法只提供發佈請求。你需要提交表單提交到這個URL或更改@RequestMapping(value =「/ addToken/{token}/imei/{imei}」,method = RequestMethod.GET) –

回答

0

setUserToken方法只提供發佈請求。您需要使用POST請求提交表單到這個網址,或者變更

@RequestMapping(value = "/addToken/{token}/imei/{imei}", method = RequestMethod.GET) 
相關問題