Frank Provost Frank Provost - 25 days ago 9
JSON Question

Jackson fails to parse nested object with exception Constructur Argument #6 has no property name annotation

I have an json input like this

{"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"
}
}
]}


I want to parse the result objects of these classes

@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;
}


I try to parse it like this

final ObjectNode node = mapper.readValue(result.getBody(), ObjectNode.class);

users = mapper.readValue(node.get("results").toString(), mapper.getTypeFactory().constructCollectionType(List.class, StagingUser.class));


The error is

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

Answer

I think the problem is that the nested class is not static. For non-static nested classes, an extra parameter containing a reference to the instance of the outer class is introduced. The @ConstructorProperties does not have a name for this parameter, that is prepended by the compiler to the list of parameters. Actually, all names in @ConstructorProperties are off by one.

Did you know that you can also put @Getter and @Setter on the class?