简体   繁体   中英

request not passed to rest controller in spring

I have my rest controller like this:

@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);
    }
}

And the deployment descriptor:

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

Here is the 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 call:

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

The page is opened as a static file and when I click on submit I am sending a ajax request to the rest controller but however it is not getting called. If the see the output it is supposed to print this line System.out.println("form data received"); which is inside the controller.

You've missed path information /BulkSMS and add .html in your request path, try like following, may help for you;)

@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);
    }
}

Or you can remove /BulkSMS from your ajax request like this:

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

You haved mapped your servlet to "/".

You should map your servlet to "/bulkSMS/*"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM