How to manipulate data form microservice response in Java, Spring Boot [closed]

I am working on a project in Spring Boot that has a microservice architecture. I needed to make a call from one microservice to another and with data that I get as response do something. I am very new to all microservice architecture and Spring Boot, so I figured I need a small push 🙂

So I have this class:

HttpDataClient.java

public class HttpDataClient implements DataClient{

    private final static Logger LOGGER = LoggerFactory.getLogger(HttpDataClient.class);

    private final RestTemplate restTemplate;
    private final ObjectMapper objectMapper = new ObjectMapper();

    public HttpDataClient(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @Override
    public DataResponse getData(String dataId) {
        try{
            JsonNode node = restTemplate.exchange(
                    String.format("/data/%s", dataId),
                    HttpMethod.POST,
                    new HttpEntity<>(buildRequest(dataId), headers()),
                    JsonNode.class
            ).getBody();
            return dataResponse(node);
        }catch (HttpStatusCodeException e) {
            String msg = String.format(
                    "Error getting data for dataId: %s",
                    dataId,
                    e.getStatusCode(),
                    e.getResponseBodyAsString());
            LOGGER.error(msg);
            return dataResponse.failed();
        }
    }

    private MultiValueMap<String, String> headers() {
        final LinkedMultiValueMap<String, String> mv = new LinkedMultiValueMap<>();
        mv.set(HttpHeaders.CONTENT_TYPE, "application/json");
        return mv;
    }

    private DataResponse dataResponse(JsonNode node) {
        return DataResponse.dataResponse(
                asString(node, "dataId"),
                asString(node, "author"),
                asString(node, "authorDataId"),
                asString(node, "serverSideDataId")
        );
    }

    private JsonNode buildRequest(String dataId) {
        ObjectNode root = objectMapper.createObjectNode();
        root.put("dataId", dataId);
        return root;
    }
}

And the interface

public interface DataClient {

    DataResponse getData(String dataId);
}

And from this class when I get response I should be able to do next if author is “Philadelphia” then athorDataId and serverSideId are the same and if author is not “Philadelphia” then athorDataId and serverSideId are not the same.

For now, I have created this class:

public class DataResolver {

    private final HttpDataClient client;

    public DataResolver(HttpDataClient client) {
        this.client = client;
    }
}

And in this class, I should execute all of this:

And from this class when I get response I should be able to do next if author is “Philadelphia” then athorDataId and serverSideId are the same and if author is not “Philadelphia” then athorDataId and serverSideId are not the same.

But I don’t know how to start. I know I suppose to use client to get the response data. I am not sure how to use it.. And then I should probably do something like this :

if (author == Philadelphia) {
        authorDataId == serverSideDataId
    } elseif(author != Philadelphia) {
        authorDataId != serverSideDataId
    }

UPDATE

Is it possible to create new class like this

public class DataResolver {

    private final HttpDataClient client;

    public DataResolver(HttpDataClient client) {
        this.client = client;
    }
}

And in this class access data from response and manipulate it in some way?

Something like this:

public class DataResolver {
    private final HttpDataClient client;

    public DataResolver(HttpDataClient client) {
        this.client = client;
    }

    public DataIdResolver idResolver() {
        if (author == Philadelphia) {
            authorDataId == serverSideDataId
        } elseif(author != Philadelphia) {
            authorDataId != serverSideDataId
        }
    }
}

Answer

If the response that you expect should contain the mentioned attributes, then you should create the following class and use it as the return type in restTemplate.exchange instead of JsonNode.class (assuming you have Jackson in your classpath):

public class DataClient {
   private String dataId;
   private String author;
   private String authorDataId;
   private String serverSideDataId;

   // getters and setters
}

So you would have something like in the HttpDataClient class:

@Override
public DataClient getData(String dataId) {
    try{
        DataClient data = restTemplate.exchange(
                String.format("/data/%s", dataId),
                HttpMethod.POST,
                new HttpEntity<>(buildRequest(dataId), headers()),
                DataClient.class).getBody();
        return data;
    }catch (HttpStatusCodeException e) {
        String msg = String.format(
                "Error getting data for dataId: %s",
                dataId,
                e.getStatusCode(),
                e.getResponseBodyAsString());
        LOGGER.error(msg);
        return dataResponse.failed();
    }
}

And DataResolver:

@Component
public class DataResolver {
   private final HttpDataClient client;

   public DataResolver(HttpDataClient client) {
      this.client = client;
   }

   public DataClient idResolver(String dataId) {
       DataClient data = client.getData(dataId);
       // Whatever logic you need
       return data;
   }
}

Maybe one important thing here is that you most likely would want to make HttpDataClient a Spring bean by adding the @Service annotation. By doing this you can autowire it to any other Spring bean you need.