java - SpringBoot - 删除方法给出此异常 -"exception":"org.springframework.http.converter.HttpMessageNotReadableException"

标签 java spring spring-boot

我是 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/

相关文章:

Java : Why does placement of static variable matter?

java - Spring通过包名进行日志记录

java - 读提交和幻读(Spring)

java - 将现有的 Jersey Postgres 应用程序迁移到 Spring Boot

java - spring boot Controller 测试,mockMov 不模拟

java - 有条件地加载应用程序上下文

java - 如何在字节缓冲区中表示目标列表

java - SQLite - Java - onCreate 和 onUpgrade - Android

java - 使用 spring boot 和 mongodb 检索具有完整和不完整配置文件的用户数量的最佳方法是什么?

java - Spring 启动应用程序。使用 Intellij 运行会导致在相同的类上出现 ClassCastException