我从弹簧控制器得到了错误的请求问题。 (原因:客户端发送的请求在语法上是不正确的。) 以下是我的休息电话:
请求的有效负载:
categoty: "Game"
itemDescription: "adas"
itemDiscount: 1
itemName: "asdas"
itemPrice: 1
itemQuantity: 1
休息方法
@RequestMapping(value="/addItem", method = RequestMethod.POST,headers="Accept=application/json")
public @ResponseBody String createItem(@RequestBody Item item) {
ItemDAO itemDAO=new ItemDAO(item);
try{
itemDAO.createProduct();
return "Success";
}catch(Exception e){
System.out.println("Exception occured in item update");
e.printStackTrace();
return "Exception occured in item add";
}
}
模型(仅属性)
@Entity
@Table(name= "Item")
public class Item {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id;
private String itemName,itemDescription,categoty;
private double itemPrice;
private float itemDiscount;
private int itemQuantity;
}
标记
<div data-ng-show="show=='addProduct'">
<p class="{{errorClass}}">{{newProductError}}</p>
<h2>Add an Item</h2>
<form class="form-horizontal" role="form">
<div class="form-group">
<label class="control-label col-sm-2" for="email">Item Name</label>
<div class="col-sm-10">
<input class="form-control" placeholder="Item Name" ng-model="newItem.itemName">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="email">Item Description</label>
<div class="col-sm-10">
<input class="form-control" placeholder="Item Description" ng-model="newItem.itemDescription">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="email">Item Price</label>
<div class="col-sm-10">
<input class="form-control" type="number" placeholder="Item Price" ng-model="newItem.itemPrice">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Item Discount</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="pwd" placeholder="Item Discount" ng-model="newItem.itemDiscount">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Item Quantity</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="pwd" placeholder="Item Quantity" ng-model="newItem.itemQuantity">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="email">Item Category</label>
<div class="col-sm-10">
<input class="form-control" placeholder="Item Category" ng-model="newItem.categoty">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success" ng-click="addItem();">Submit</button>
</div>
</div>
</form>
</div>
服务
addItem: function(item){
return $http({
method : 'POST',
url : '/Ecomm-Java-Backend/item/addItem',
data: item,
heades:{
'Content-type': 'application/json'
}
}).success(function(result){
console.log(result.length);
console.log("result success "+ result);
return result;
}).error(function(err){
console.log("result success "+ err);
return err;
});
}
当我调用其余的电话时,我收到了错误的请求错误。
这可能是什么问题?任何帮助都会受到赞赏。
答案 0 :(得分:0)
在Spring控制器中试试这个
@RequestMapping(value="/addItem", method = RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String createItem(@RequestBody Item item) {
ItemDAO itemDAO=new ItemDAO(item);
try{
itemDAO.createProduct();
return "Success";
}catch(Exception e){
System.out.println("Exception occured in item update");
e.printStackTrace();
return "Exception occured in item add";
}
}
这在您的客户端
addItem: function(item){
return $http({
method : 'POST',
url : '/Ecomm-Java-Backend/item/addItem',
data: {
categoty: "Game" ,
itemDescription: "adas",
itemDiscount: 1 ,
itemName: "asdas",
itemPrice: 1,
itemQuantity: 1
// get values from item object
},
headers : { 'Accept': 'application/json',
'Content-Type': 'application/json'
}
}).success(function(result){
console.log(result.length);
console.log("result success "+ result);
return result;
}).error(function(err){
console.log("result success "+ err);
return err;
});
}
答案 1 :(得分:0)
终于解决了 将json数据更改为
categoty: "Game"
description: "test1"
discount: 10
name: "Test"
price: 10
quantity: 10
但仍然无法弄清楚为什么会有效...