gpt4 book ai didi

java - Spring 请求未传递给休息 Controller

转载 作者:行者123 更新时间:2023-12-02 03:37:26 25 4
gpt4 key购买 nike

我的休息 Controller 是这样的:

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

页面作为静态文件打开,当我单击提交时,我向其余 Controller 发送 ajax 请求,但它没有被调用。如果看到输出,则应该打印 Controller 内部的这一行 System.out.println("form data returned"); 。

最佳答案

您已将 servlet 映射到“/”。

您应该将 servlet 映射到“/bulk SMS/*”

关于java - Spring 请求未传递给休息 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37272270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com