I have the following JSON I am receiving from a REST call:
"socialConnectionsData" : {
"friends" : [ {
"friend" : {
"firstname" : "John",
"lastname" : "Doe"
}
}]
}
My current class looks like this:
public class SocialConnectionsData {
private List<Friend> friends;
public List<Friend> getFriends() {
return friends;
}
public void setFriends(List<Friend> friends) {
this.friends = friends;
}
}
However, I get an Jackson exception stating that “friend” is an unrecognized field. How would I have a collection of Friends and maintain the type of Friend?
Looks to me like you have an extra property name in there. Jackson is expecting JSON like this to correspond to your Objects:
You shouldn’t need to do anything to maintain the type of Friend because your list is declared with that as a generic parameter.
If you want to go the other way around and make your objects match the JSON, then you’ll need an extra level of indirection like so: