我是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;
}
我的控制器:
@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-给出此异常的Delete方法-“exception”:“org.springframework.http.converter.HttpMessageNotReadableException”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46922566/