Home > Back-end >  Filtering response using webclient
Filtering response using webclient

Time:06-22

I'm new using webclient to cosume a Rest API and I want to know how can I filter a response to match only what I want.

So I have this endpoint which brings me a customerById but I need to show only the the systemsId = 100

Let me show:

@GetMapping("/getCucoId/{cucoId}")
    public Mono<CuCoPerson> getCucoRelationById(@PathVariable Integer cucoId) {

        WebClient webClient = WebClient.create();

        return webClient.get()
                .uri(GET_RELATION_BY_ID   cucoId)
                .header("Accept", "application/json")
                .header("Authorization", "Bearer eyJraWQiOi.....")
                .retrieve()
                .bodyToMono(CuCoPerson.class);
    }

And the POJO:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CuCoPerson {

    private Integer cucoID;
    private List<CustomerRelation> relatedCustomers;
}

And this is the response in Postman:

{
    "cucoID": 100288298,
    "relatedCustomers": [
        {
            "customerId": "F6305957",
            "systemId": 100
        },
        {
            "customerId": "F8364917",
            "systemId": 400
        },
        {
            "customerId": "F4194868",
            "systemId": 101
        }
    ]
}

So I need only to show the relatedCustomers who only have a systemID = 100

Thanks in advance!

CodePudding user response:

I think you are looking for either Mono.map(), or Mono.flatMap().

Remove the customers, who don't match in the function, and return the changed object.

@GetMapping("/getCucoId/{cucoId}")
public Mono<CuCoPerson> getCucoRelationById(@PathVariable Integer cucoId) {
  WebClient webClient = WebClient.create();
  return webClient.get()
          .uri(GET_RELATION_BY_ID   cucoId)
          .header("Accept", "application/json")
          .header("Authorization", "Bearer eyJraWQiOi.....")
          .retrieve()
          .bodyToMono(CuCoPerson.class)
          .map(cuCoPerson -> {
            List<CustomerRelation> matches = cuCoPerson.getRelatedCustomers()
                    .stream()
                    .filter(relation -> relation.getSystemId().equals(100))
                    .collect(Collectors.toList());
            cuCoPerson.setRelatedCustomers(matches);
            return cuCoPerson;
          });
}

As mentioned in other answers, doing this kind of filtering on client side is bad practice. If possible the API should expose parameter for systemId and return only the data you need.

CodePudding user response:

First of all this is bad Backend design. Since you (client side) have the need for such info you should have an endpoint available to get your info by customer ID and System ID. Data filtering should be done on the backend and the client should remain as thin as possible. Otherwise you perform logic both on server-side and client-side plus you send some completely unneeded info over the network and then filter it out on the client side. But if that's your available resources and you can't change the server-side then you you take your JSON response, and parse it into your class that maps to that JSON or just into Map<String,Object> if you don't have the class for that JSON and filter it out yourself. To parse a json string to map or a specific POJO you can use Jackson library - method readValue() of ObjectMapper Or Gson library. Also, I wrote my own simple wrapper over Jackson library that simplifies the use. Class JsonUtils is available as part of MgntUtils library (written and maintained by me). This class just has the methods that allow you to parse and de-serialize from/to Json String from/to a class instance. Here is a simple example on how to parse your JSON string into a Map<String,Object> with JSONUtils class

Map<String,Object> myMap    
try {   
  myMap = JsonUtils.readObjectFromJsonString(myJsonStr, Map.class);
} catch(IOException ioe) {
  ....
}

MgntUtils library available as Maven artifact and on Github (including source code and Javadoc)

CodePudding user response:

it could be something like this:

return webClient.get()
      .uri("/someUrl")
      .header("Accept", "application/json")
      .header("Authorization", "Bearer eyJraWQiOi.....")
      .retrieve()
      .bodyToFlux(CuCoPerson.class)
      .filter(cuCoPerson -> cuCoPerson.getRelatedCustomers().stream().anyMatch(cr -> cr.getSystemId() == 100))
      .take(1)
      .next();

But this has disadvantage that you are filtering the results on client side (your application). I would try to ask the api provider if s/he is able to provide filter parameter (something like systemId) and in that case you would call the endpoint with that query param.

  • Related