1

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?

1
  • please don't use block to retrieve a value from a publisher like this. you instantly turned your application to be not reactive anymore, so you can just write it procedurally. Commented Aug 10, 2021 at 7:22

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.