How to get a json field by name using Spring WebClient?

前端 未结 1 1012
花落未央
花落未央 2021-01-14 16:09

I have the following JSON Response:

{
    \"Count\": 1,
    \"Products\": [
        {
            \"ProductID\": 3423
        },
        {
            \"Prod         


        
1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-14 16:46

    after retrieve() you can always .map your result to corresponding type. With the help of JsonNode path() instance method you can do it similar to WebTestClient jsonPath()

    webClient.get()
                .uri(uriBuilder -> uriBuilder
                    .path(URI_PRODUCTS)
                    .build())
                .accept(MediaType.APPLICATION_JSON)
                .retrieve()
                .bodyToMono(JsonNode.class)
                .map(s-> s.path("Products"))
                .map(s->{
                    try {
                        return mapper.readValue(s.traverse(), new TypeReference>() {} );
                    } catch (IOException e) {
                        e.printStackTrace();
                        return new ArrayList();
                    }
                })
                .block();
    

    0 讨论(0)
提交回复
热议问题