Jackson 无法解析嵌套对象,异常 Constructur Argument #6 has no 属性 name annotation
Jackson fails to parse nested object with exception Constructur Argument #6 has no property name annotation
我有一个 json 这样的输入
{"results" : [
{
"__metadata": {
"type": "SFOData.User"
},
"userId": "1",
"lastName": "TestUser",
"division": "Manufacturing (MANU)",
"department": "Production FR (123123123)",
"firstName": "Example",
"manager": {
"__metadata": {
"type": "SFOData.User"
},
"userId": "321321321"
}
}
]}
我要解析这些类
的结果对象
@JsonIgnoreProperties(ignoreUnknown = true)
@AllArgsConstructor
@NoArgsConstructor
private class StagingUser {
@Getter
@Setter
private Long userId;
@Getter
@Setter
private String lastName;
@Getter
@Setter
private String division;
@Getter
@Setter
private String firstName;
@Getter
@Setter
private String department;
@Getter
@Setter
private StagingManager manager;
}
@JsonIgnoreProperties(ignoreUnknown = true)
@AllArgsConstructor
@NoArgsConstructor
private class StagingManager {
@Getter
@Setter
private Long userId;
}
我试着这样解析
final ObjectNode node = mapper.readValue(result.getBody(), ObjectNode.class);
users = mapper.readValue(node.get("results").toString(), mapper.getTypeFactory().constructCollectionType(List.class, StagingUser.class));
错误是
Argument #6 of constructor [constructor for my.package.Obj$StagingUser, annotations: {
interface java.beans.ConstructorProperties=@java.beans.ConstructorProperties(
value=[userId, lastName, division, firstName, department, manager]
)
}]
has no property name annotation; must have name when multiple-parameter constructor annotated as Creator
我认为问题在于嵌套 class 不是静态的。对于非静态嵌套 classes,引入了一个额外的参数,其中包含对外部 class 实例的引用。 @ConstructorProperties
没有此参数的名称,编译器将其添加到参数列表中。实际上,@ConstructorProperties
中的所有名称都相差一个。
您知道吗,您还可以将 @Getter
和 @Setter
放在 class 上?
我有一个 json 这样的输入
{"results" : [
{
"__metadata": {
"type": "SFOData.User"
},
"userId": "1",
"lastName": "TestUser",
"division": "Manufacturing (MANU)",
"department": "Production FR (123123123)",
"firstName": "Example",
"manager": {
"__metadata": {
"type": "SFOData.User"
},
"userId": "321321321"
}
}
]}
我要解析这些类
的结果对象@JsonIgnoreProperties(ignoreUnknown = true)
@AllArgsConstructor
@NoArgsConstructor
private class StagingUser {
@Getter
@Setter
private Long userId;
@Getter
@Setter
private String lastName;
@Getter
@Setter
private String division;
@Getter
@Setter
private String firstName;
@Getter
@Setter
private String department;
@Getter
@Setter
private StagingManager manager;
}
@JsonIgnoreProperties(ignoreUnknown = true)
@AllArgsConstructor
@NoArgsConstructor
private class StagingManager {
@Getter
@Setter
private Long userId;
}
我试着这样解析
final ObjectNode node = mapper.readValue(result.getBody(), ObjectNode.class);
users = mapper.readValue(node.get("results").toString(), mapper.getTypeFactory().constructCollectionType(List.class, StagingUser.class));
错误是
Argument #6 of constructor [constructor for my.package.Obj$StagingUser, annotations: {
interface java.beans.ConstructorProperties=@java.beans.ConstructorProperties(
value=[userId, lastName, division, firstName, department, manager]
)
}]
has no property name annotation; must have name when multiple-parameter constructor annotated as Creator
我认为问题在于嵌套 class 不是静态的。对于非静态嵌套 classes,引入了一个额外的参数,其中包含对外部 class 实例的引用。 @ConstructorProperties
没有此参数的名称,编译器将其添加到参数列表中。实际上,@ConstructorProperties
中的所有名称都相差一个。
您知道吗,您还可以将 @Getter
和 @Setter
放在 class 上?