SpringBoot - 提供此异常的Delete方法 - " exception":" org.springframework.http.converter.HttpMessageNotReadableException"

时间:2017-10-25 01:36:19

标签: java spring spring-boot

我是Springboot的新手,并试图了解它是如何工作的。我正在构建一个小应用程序,其中API的delete方法给了我这个错误。

{
    "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();
}

添加功能和更新工作正常,但不确定删除失败的原因。

1 个答案:

答案 0 :(得分:0)

我在该代码中发现了这个问题。

我试图通过传递shoppingListId来删除该项目。

然后我通过传入整个ShoppingList对象并从该对象访问id来更新我的服务。

@RequestMapping(method=RequestMethod.DELETE, value="/shoppinglist")
    public void deleteShoppingList(@RequestBody ShoppingList shoppingList) {
        this.service.deleteShoppingList(shoppingList.getShoppingListId());
    }

它对我有用。

谢谢!