Reputation: 2592
I'm having some trouble mapping a GraphQL response to a class and I'm probably missing something obvious so I need a second pair of eyes on my code I think.
I have a response that looks like this:
{
"data": {
"Area": [
{
"id": "1"
},
{
"id": "2"
}
]
}
}
My consumer and client looks like this:
public class Consumer {
private final WebClient webClient;
private static final String QUERY = "query { Area { id }}";
public Consumer(WebClient webClient) {
this.webClient = webClient;
}
public AreaResponse.AreaData getAreas() {
var request = new GraphQLRequest(QUERY, null);
var res = webClient.post()
.bodyValue(request)
.retrieve()
.bodyToMono(AreaResponse.class)
.block();
return res.getData();
}
}
And finally my response dto:
@Data
public class AreaResponse {
private AreaData data;
@Data
public class AreaData{
private Collection<Area> Area;
@Data
public class Area{
private String id;
}
}
}
If I map the response to String.class
I get the same data as in Postman so there's nothing wrong with the query or the endpoint. But when I try to map to AreaResponse
the Area
object is null. I am not sure if the hierarchy in my response class is correct or if I should move the Collection a step down?
Upvotes: 1
Views: 2217
Reputation: 1
I think the problem comes from the use of upper-case Area
in the response. By default, Jackson will try to map all of the stuff to a lower-case version of the class name (or lower-camelcase).
One way to fix it:
@JsonProperty("Area")
private Collection<Area> Area;
P.S. nested/ inner classes can be static, so public static class AreaData
etc.
Upvotes: 1