Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t反序列化从Springboot post映射函数上的角度http post请求发送的POJO_Java_Json_Angular_Spring Boot_Jackson - Fatal编程技术网

Java Can';t反序列化从Springboot post映射函数上的角度http post请求发送的POJO

Java Can';t反序列化从Springboot post映射函数上的角度http post请求发送的POJO,java,json,angular,spring-boot,jackson,Java,Json,Angular,Spring Boot,Jackson,在上下文中,我的应用程序是一个咖啡馆,我想向我的springboot后端发送一系列项目。然而,jackson给出了一个例外: Cannot construct instance of `me.andrewq.coffeeshop.menu_items.Menu` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based

在上下文中,我的应用程序是一个咖啡馆,我想向我的springboot后端发送一系列项目。然而,jackson给出了一个例外:

Cannot construct instance of `me.andrewq.coffeeshop.menu_items.Menu` 
(no Creators, like default constructor, exist): cannot deserialize from Object value 
(no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 1, column: 3] (through reference chain: 
java.util.ArrayList[0])] with root cause
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 
Cannot construct instance of `me.andrewq.coffeeshop.menu_items.Menu` 
(no Creators, like default constructor, exist): cannot deserialize from Object value 
(no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 1, column: 3] (through reference chain: 
java.util.ArrayList[0]).
这是项的类的外观(省略setter和getter之后):

http请求调用是:
this.http.post(`${this.url}/guestOrder`,this.orderItems)其中
http:HttpClient
orderItems:Menu[]

如果不格式化JSON,JSON字符串的第65列将出现错误:

[{"productId":1,"name":"Iced Coffee","price":2,"productOptions":[["S","2.00"],["M","2.50"],["L","3.00"]],"type":"IC","currentSize":"S","creams":0,"sugars":0}]

这是
productOptions

的第一个括号,例外情况实际上说明了这一点——您需要在POJO类中添加一个默认构造函数

JSON解析器首先创建一个空实例,然后为JSON文本中遇到的每个属性调用setter方法。JSON中不包含的属性保持不变,因此具有默认构造函数为其指定的值(通常
null
,除非将其设置为其他值)


我希望为了清楚起见,您所说的是getter和setter,它们确实存在,否则就无法正常工作。

您确定这就是正在传递的JSON字符串吗?您提到的异常表示它希望将
productOptions
元素解析为字符串,而不是数组,这在您给出的JSON中没有反映出来。另外,它说了一些关于第1行第65列的内容,但是第1行没有第65列,这使得调试更加困难(也许你已经格式化了JSON?如果是,请给出原始的未格式化文本)。是的,对不起,在这里发布之前,我已经格式化了文本的可读性,但是我插入了文章底部正文中实际的JSON行。显然,错误发生在第一个开口括号“[”处感谢您的澄清。在本例中,JSON与错误消息中的位置相匹配,但仍然没有解释为什么它要在解析数组的位置解析字符串。JSON看起来很好,似乎与您的POJO相匹配。您的Java代码中是否有
productOptions
e作为普通字符串(而不是
String[][]
)在早期版本中,没有正确地重新编译或重新部署?我运行了
mvn clean
,然后运行了
mvn spring boot:run
,错误实际上已更改为与无法创建
菜单
对象有关的内容。我将在下一个异常中编辑到帖子中。感谢您的新输入-现在它已实际运行比第一次容易多了:-)也请修正你的文章格式-你一定是在编辑时弄乱了结尾的格式标签。第一次很好。
@RestController
public class OrderController {
    
    @CrossOrigin(origins = "http://localhost:4200")
    @PostMapping(path = "/guestOrder")
    public String order(@RequestBody List<Menu> order){
        
        for(Menu item: order){
            System.out.println(item.toString());
        }
        
        return "Sending order worked";
    }
}
export interface Menu {
    productId: number;

    name: string;
    
    price: number;
    
    productOptions: string[][];
    
    type: string;

    // additional field for drinks and coffees
    currentSize: string;

    creams: number;

    sugars: number;
}
[{"productId":1,"name":"Iced Coffee","price":2,"productOptions":[["S","2.00"],["M","2.50"],["L","3.00"]],"type":"IC","currentSize":"S","creams":0,"sugars":0}]