JSON result as String or Mono<T> in Spring WebClient?

Viewed 37

I have just started to use Spring WebClient and have troubles for retrieving data properly. Here is a request that works without any problem when I get JSON result as String:

String response = webClient.get()
                .uri("/produts?store={store}", store)
                .retrieve()
                .bodyToMono(String.class)
                .block();

However, in many examples, the return type is Mono<T> and I also need to map JSON result to the related Model class. Here is the default way:

Mono<Product> response = webClient.get()
                .uri("/produts?store={store}", store)
                .retrieve()
                .bodyToMono(Product.class);

Here is the model that I want to map from Mono<T> result:

@Getter
public class Product {

    @JsonProperty("id")
    private int id;

    @JsonProperty("product_name")
    private String productName;

    @JsonProperty("buying_date")
    private LocalDate buyingDate;

    // other properties
}

So, how can I map the API result to my Product model properly? I can also use some Json Mapper e.g. GSON, etc. but I want to use the most proper way when using Spring WebClient.

0 Answers
Related