我是 Springboot 的新手,正在尝试了解它是如何工作的。我正在构建一个小型应用程序,其中 API 的删除方法给了我这个错误。
{
"timestamp":1508894413495,
"status":400,
"error":"Bad Request",
"exception":"org.springframework.http.converter.HttpMessageNotReadableException",
"message":"JSON parse error: Can not deserialize instance of int out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of int out of START_OBJECT token at [Source: java.io.PushbackInputStream@5685db7d; line: 1, column: 1]",
"path":"/shoppinglist"
}
我的构造函数:
private String title; private int shoppingListId;
public int getShoppingListId() {
return shoppingListId;
}
public void setShoppingListId(int shoppingListId) {
this.shoppingListId = shoppingListId;
}
我的 Controller :
@RequestMapping(method=RequestMethod.DELETE, value="/shoppinglist")
public void deleteShoppingList(@RequestBody int shoppingListId) {
this.service.deleteShoppingList(shoppingListId);
}
我的服务:
private List<ShoppingList> shoppingLists;
public ShoppingListService() {
this.shoppingLists = new ArrayList<ShoppingList>();
this.shoppingLists.add(new ShoppingList(1, "HEB"));
this.shoppingLists.add(new ShoppingList(2, "Walmart"));
this.shoppingLists.add(new ShoppingList(3, "Market Basket"));
this.shoppingLists.add(new ShoppingList(4, "Kroger"));
}
public void deleteShoppingList(int shoppingListId) {
ShoppingList shoppingList = getShoppingListById(shoppingListId);
this.shoppingLists.remove(shoppingList);
}
public ShoppingList getShoppingListById(int shoppingListId) {
return this.shoppingLists.stream().filter(x -> x.getShoppingListId() == shoppingListId).findFirst().get();
}
添加功能和更新工作正常,但不确定为什么删除失败。
最佳答案
我发现了该代码中的问题。
我试图通过传入 shoppingListId 来删除该项目。
然后,我通过传入整个 ShoppingList 对象并访问该对象的 ID 来更新我的服务。
@RequestMapping(method=RequestMethod.DELETE, value="/shoppinglist")
public void deleteShoppingList(@RequestBody ShoppingList shoppingList) {
this.service.deleteShoppingList(shoppingList.getShoppingListId());
}
这对我有用。
谢谢!
关于java - SpringBoot - 删除方法给出此异常 -"exception":"org.springframework.http.converter.HttpMessageNotReadableException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46922566/