java Spring-Boot REST - 所需的字符串参数不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37404037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring-Boot REST - Required String parameter is not present
提问by Brandon Hewlett
So, I'm trying to to a POST request to a local Spring MVC project, but every time I try, I keep getting 400: Bad Request, with the error specified in the title. Not sure why, because when I check what's being sent, it's correct.
因此,我正在尝试向本地 Spring MVC 项目发送 POST 请求,但每次尝试时,我都会收到 400: Bad Request,并在标题中指定错误。不知道为什么,因为当我检查发送的内容时,它是正确的。
Spring REST controller request
Spring REST 控制器请求
@RequestMapping(value="/add", method = RequestMethod.POST)
void addPet(@RequestParam String name, @RequestParam String photo, @RequestParam String status) {
Pet pet = new Pet(name, photo, status);
this.petRepo.save(pet);
}
AngularJS request sending the data
AngularJS 请求发送数据
$http({
method: "post",
url: "http://localhost:8080/pet/add",
data:{
name: angular.element("#name")[0].value,
photo: angular.element("#photo")[0].value,
status: angular.element("#status")[0].value
}
}).success(function(data, status) {
})
.error(function(data, status) {
alert("Error");
console.log(data);
console.log(status);
});
front-end HTML that's calling the AngularJS function
调用 AngularJS 函数的前端 HTML
<div class="form-group">
<label for="name">Pet Name</label>
<input type="text" class="form-control" id="name">
</div>
<div class="form-group">
<label for="photo">Photo URL</label>
<input type="text" class="form-control" id="photo">
</div>
<div class="form-group">
<label for="status">Status</label>
<input type="text" class="form-control" id="status" placeholder="Something descriptive like 'Available' or 'Taken'">
</div>
<div class="form-group">
<div class="btn-group">
<button type="button" class="btn btn-primary" ng-click="preview()">Preview</button>
<button type="button" class="btn btn-success" ng-click="submit()">Submit</button>
<button type="button" class="btn btn-danger" ng-click="clear()">Clear</button>
</div>
</div>
回答by niekname
You are using @RequestParam in the backend but in angular you are putting the values in the body of the request.
您在后端使用 @RequestParam 但在 angular 中,您将值放在请求正文中。
You have to use @RequestBody in the backend or adjust the frontend to put the values in url parameters.
您必须在后端使用 @RequestBody 或调整前端以将值放入 url 参数中。
回答by Brandon Hewlett
Found the answer. I followed SSH's answer above for the front-end, and for the backend, I changed the function to:
找到了答案。对于前端,我按照上面的 SSH 回答进行操作,对于后端,我将函数更改为:
void addPet(@RequestBody Pet pet)
回答by Hadi J
try like this
像这样尝试
<div class="form-group">
<label for="name">Pet Name</label>
<input type="text" class="form-control" ng-model="form.name" id="name">
</div>
<div class="form-group">
<label for="photo">Photo URL</label>
<input type="text" class="form-control" ng-model="form.photo" id="photo">
</div>
<div class="form-group">
<label for="status">Status</label>
<input type="text" class="form-control" ng-model="form.status" id="status" placeholder="Something descriptive like 'Available' or 'Taken'">
</div>
<div class="form-group">
<div class="btn-group">
<button type="button" class="btn btn-primary" ng-click="preview()">Preview</button>
<button type="button" class="btn btn-success" ng-click="submit(form)">Submit</button>
<button type="button" class="btn btn-danger" ng-click="clear()">Clear</button>
</div>
</div>
and in controller use this
并在控制器中使用它
$scope.form = {"name":"","photo":"","status":""}
$http({
method: "post",
url: "http://localhost:8080/pet/add",
data:{
name: $scope.form.name,
photo: $scope.form.photo,
status: $scope.form.status
}
}).success(function(data, status) {
})
.error(function(data, status) {
alert("Error");
console.log(data);
console.log(status);
});