请求在春天没有传递给休息控制器

时间:2016-05-17 09:30:19

标签: java spring spring-mvc spring-restcontroller

我的休息控制器是这样的:

@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>

Dispatcher 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");

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

您已将您的servlet映射到&#34; /&#34;。

您应该将servlet映射到&#34; / bulkSMS / *&#34;

相关问题