2016-05-17 60 views
0

我有我的休息控制器是這樣的:請求不會傳遞到其餘的控制器在春天

@RestController 
public class SmsRestController { 

    @RequestMapping(value = "/sendSMS", method = RequestMethod.POST, consumes = "application/json") 
    public ResponseEntity sendMessage(SmsBean smsBean) { 
     System.out.println("form data received"); 
     return new ResponseEntity("Messages Sent", HttpStatus.OK); 
    } 
} 

以及部署描述符:

<?xml version="1.0" encoding="UTF-8"?> 

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
     version="3.1"> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 

    <display-name>BulkSMS2</display-name> 
    <welcome-file-list> 
     <welcome-file>index.html</welcome-file> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 

    <servlet> 
     <servlet-name>BulkSMS2</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>BulkSMS2</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

調度的servlet:

<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:component-scan base-package="com.nt.beans" /> 
    <mvc:default-servlet-handler/> 
    <mvc:annotation-driven /> 

    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value></value> 
     </property> 
     <property name="suffix"> 
      <value>.html</value> 
     </property> 
    </bean> 
</beans> 

這裏html:

<div class="form-group" ng-controller="sendSMSController"> 
     <form name="myForm2" ng-submit="sendSMS()" method="POST" novalidate> 
      <div class="form-group"> 
       <label for="comment">Message:</label> 
       <textarea class="form-control" rows="2" id="comment" ng-model="message" placeholder="Enter your message here"></textarea> 
      </div> 
      <input type="submit" class="form-control btn btn-success" value="Submit" /> 
      <h5 style="color: red; font-weight: bold">{{error}}</h5> 
     </form> 
    </div> 
</div> 

Ajax調用:

$http.post('/BulkSMS/sendSMS', data).success(success).error(error); 

頁打開作爲靜態文件,當我點擊提交我發送一個Ajax請求休息控制器,但但它是沒有得到調用。如果看到輸出,它應該打印在控制器內部的這條線System.out.println("form data received");

回答

1

你已經錯過了路徑信息/BulkSMS並在請求路徑添加.html,嘗試像以下,可能會幫助你;)

@RestController 
@RequestMapping(value = "/BulkSMS") 
public class SmsRestController { 

    private String API_KEY = "http://smshorizon.co.in/api/sendsms.php?user=Kiriti&apikey=mktJTXhxraasxvrDsEjt&mobile=xxyy&message=xxyy&senderid=xxyy&type=txt"; 

    @RequestMapping(value = "/sendSMS", method = RequestMethod.POST, consumes = "application/json") 
    public ResponseEntity sendMessage(SmsBean smsBean) { 
     System.out.println("form data received"); 
     return new ResponseEntity("Messages Sent", HttpStatus.OK); 
    } 
} 

或者你可以從你的Ajax請求刪除/BulkSMS這樣的:

$http.post('/sendSMS.html', data).success(success).error(error); 
+0

沒有work..Throwing錯誤:通過'http://本地主機:8084/BulkSMS2/sendSMS無法加載資源:服務器與迴應狀態404(Not Found)' – Satyadev

+0

好的,你有'suffix' ='.html',所以你應該把它加入你的ajax調用中。請再次檢查我的答案。 – Blank

+0

類似的錯誤:'http:// localhost:8084/sendSMS.html無法加載資源:服務器通過這種方式迴應狀態爲404(Not Found)' – Satyadev

1

您已將您的servlet映射到「/」。

您應該映射你的servlet 「/ bulkSMS/*」

+0

像這樣:'servlet-mapping> BulkSMS2 /BulkSMS2/* '? – Satyadev

+0

嘗試像我上面的評論和控制檯中的拋出錯誤:'http:// localhost:8084/BulkSMS2/sendSMS無法加載資源:服務器響應狀態爲404(未找到)' – Satyadev