实际上我想将Spring与Angularjs集成,我是Angularjs的初学者。当我使用$ http post方法时,它返回POST 405.Here是我的代码
SignUp.html
<form method="post" ng-submit="saveUser()">
<input type="hidden" name="${_csrf.parameterName}"
value="${_csrf.token}" />
</form>
myApp.js
var myApp = angular.module('myApp', ['ngMessages']);
UserIdController.js
myApp.controller('UserIdController', [
'$scope',
'UserIdService',
function($scope, UserIdService) {
$scope.saveUser = function() {
UserIdService.saveUser($scope.user).then(
function success(response) {
console.log("user Added");
}, function error(response) {
console.log("User not Added")
});
}
} ]);
UserIdService.js
myApp
.service(
'UserIdService',
[
'$http',
function($http) {
this.saveUser = function saveUser(user) {
return $http({
method : 'post',
url : 'SignUpUser',
data : {
firstName : user.firstName
},
headers : 'Content-Type: application/json'
});
}
} ]);
Spring REST控制器
@RestController
@Produces("text/plain")
public class SignUpController {
private static final Logger logger =
LogManager.getLogger(SignUpController.class);
@RequestMapping(value = "/SignUpUser", method =
RequestMethod.POST, headers = "Accept=application/json")
public String saveUser(@PathVariable String firstname) throws
ParseException {
logger.debug("Enter in SignUp Controller in Post Method");
UserDetails userDetails = new UserDetails();
studentDetails.setFirstName(firstname);
return "ok";
}
我在Chrome中的Inspect元素
中出现此错误 Request URL:http://localhost:8080/examapp/signup
Request Method:POST
Status Code:405
Remote Address:[::1]:8080
Referrer Policy:no-referrer-when-downgrade
Response Headers
view parsed
HTTP/1.1 405
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Allow: GET
Content-Type: text/html;charset=ISO-8859-1
Content-Language: en
Content-Length: 1084
Date: Fri, 04 Aug 2017 05:50:32 GMT
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:26
Content-Type:application/json
Cookie:JSESSIONID=ACF3E30CB7B3A6A9862F923F42DB61B5
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/examapp/signup.html
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36
Request Payload
有没有人知道我做错了什么?这似乎是一个非常直接的实现,正在处理我已经看到的所有教程。任何帮助将不胜感激。 谢谢。
答案 0 :(得分:0)
请在@RequestMapping("/examapp")
SignUpController
@RestController
@Produces("text/plain")
@RequestMapping("/examapp")
public class SignUpController {
答案 1 :(得分:0)
您在此处指定了@PathVariable。路径变量就是路径变量。你想要的是@RequestBody。
在你的场景中,Spring没有对/ SignUpUser进行任何操作,它正在寻找/ SignUpUser / {firstName},但由于@RequestMapping没有指定{firstName},你实际上是在发布一个不存在的资源。 / p>
虽然在现实世界中不切实际,只想在登录时张贴名字,但在$http.post
中,您只需要data : user.firstName
。
在你的Spring控制器中:
@RequestMapping(value = "/SignUpUser", method =
RequestMethod.POST, headers = "Accept=application/json")
public String saveUser(@RequestBody String firstname)
如果你希望你的后端接受你在Angular中的内容(更真实的世界),你会创建一个POJO类,并接受它(@RequestBody UserDetails request)
。看来你创建了UserDetails POJO,所以你只需要接受它:
@RequestMapping(value = "/SignUpUser", method =
RequestMethod.POST, headers = "Accept=application/json")
public String saveUser(@RequestBody UserDetails userDetails ) {