在我尝试与Spring一起使用的RestTemplate中,我收到了一个可选字段,如下面的示例所示。这个可选字段是一个嵌套对象,我想使用一个嵌套类来映射它。
{
"name": "John",
"age": 30
}{
"name": "John",
"age": 30,
"car": {
"year": 1984,
"color": "red"
}
}我当前的类定义:
public class User {
public class Car {
@Getter
@Setter
public String color;
@Getter
@Setter
public Integer year;
}
@Getter
@Setter
public String name;
@Getter
@Setter
public Integer age;
@Getter
@Setter
public Car car;
}通过调用:
ResponseEntity<User> response = restTemplate.exchange("http://....", HttpMethod.POST, request, User.class);我得到了以下异常:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of '....User$Car' (although at least one Creator exists): can only instantiate non-static inner class by using default, no-argument constructor
如果节点存在于json中,如何使用null或Car类实例化car属性?
发布于 2020-03-29 00:15:53
你能试试下面更新后的类吗?更改是将内部类定义为静态类。
public class User {
public static class Car {
@Getter
@Setter
public String color;
@Getter
@Setter
public Integer year;
}
@Getter
@Setter
public String name;
@Getter
@Setter
public Integer age;
@Getter
@Setter
public Car car;
}https://stackoverflow.com/questions/60903248
复制相似问题