I'm using RestTemplate to call an external API. Here is the result of the API call :
{
"results": [
{
"code": "UDE",
"usergroups": [
{
"name": "Some name",
"users": [
{
"id": 457,
"login": "uid34",
}
]
}
]
}
]
}
I'm only interested in getting the flat list of users
.
- I don't want to return a
JSON String
and usergson
to target theusers
node. - I want to avoid creating too many wrappers.
So this is what I've done so far:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
@JsonInclude(Include.NON_NULL)
@JsonRootName("usergroups")
public class UserGroup{
@JsonProperty("users")
private List<User> users = new ArrayList<User>();
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Response<T> {
@JsonProperty("results")
private List<T> results= new ArrayList<>();
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class User{
private Integer id;
private String login;
}
ResponseEntity<Response<UserGroup>> response = restTemplate.exchange(url, HttpMethod.GET, entity,
new ParameterizedTypeReference<Response<UserGroup>>(){});
List<User> users = response.getBody().getResults()
.stream()
.map(UserGroup::getUsers)
.flatMap(Collection::stream)
.collect(Collectors.toList());
The API call works fine but the users
property is always empty for all UserGroup
I have.
What am I doing wrong?
Thanks for your help.
NB : I also tried this solution but I get an exception