How to extract json field from response in declarative client in micronaut?

Several days ago I decided to give the Micronaut a try and I am highly enthusiastic. Unfortunately Javadocs are rather poor (less than terse whether rudimentary descriptions and/or no examples provided) and I’ve just hit the wall with the declarative client (which OTOH is a great thing).

So here’s the case: I declared a client for selected operations of remote 3-rd party service. Everything works as expected. Yet there’s this op, that returns JSON data in form:

{ "foos": [ {...}, {...}, ... ] }

where each object in foos array is object of the same “class”, let’s say Foo. So I got this class:

@Introspected
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class Foo {
    @JsonProperty @NonNull private String name;
}

(I omit other fields and getters/setters for brevity). In declarative client I have this:

@Client(id="third-party-service", path="/3.0")
@Header(name=HttpHeaders.ACCEPT, value="application/problem+json, application/json")
public interface ThirdPartyClient {

    @Get("/items/{item-id}/categories/{category-id}/foos") String fetchFoos(
            @NonNull @PathVariable("item-id") String itemId,
            @NonNull @PathVariable("category-id") String categoryId);
}

And fetchFoos gets the JSON as String of above described response. And this is okay.

Now what I can’t figure out is how to, declaratively, tell the client to get foos field (the array) and return it as List<Foo> instead of being forced to declare some class like FetchFoosResponse with single field foos.

I wasn’t able to find any useful example on the Internet, thou there’s pretty interesting guide on declarative clients on Micronaut site: https://guides.micronaut.io/latest/micronaut-http-client-maven-java.html – there’s a lot about configuring client’s requests, but nothing on the client’s results.

Is it even possible to do in Micronaut what I try to accomplish? (I am stuck with version 3.4.4).

Leave a Reply

Your email address will not be published.