'How to properly serialize data and keep old API contract?

I changed external API url, from old API url response had current format:

[
  {
    "id": 2222
  },
  {
    "id": 3333
  },
  {
    "id": 4444
  },
  {
    "id": 5555
  }
]

after URL change response looks like this. More fields and id type is String.

{
  "ref": {
    "id": "1111"
},
  "compset": [
    {
        "id": "2222"
    },
    {
        "id": "3333"
    },
    {
        "id": "4444"
    },
    {
        "id": "5555"
    }
],
  "isCompliant": false,
  "updateTimestamp": null,
  "remainingMilliseconds": 0,
  "totalLockoutDays": 15
}

All I need is a just compset list. Other fields can be omitted.

Response classes to serialize this json implemented in two classes.

class 1:

public class Compset {
    private List<Competitor> competitors;
}

class 2:

public class Competitor {
    @JsonProperty("id")
    private Integer id; // 
}

This code defines how to get data from old external api with feign:

public interface PropertyCompetitorConnector {
    @Headers({"Accept: application/json"})
    @RequestLine("GET /api/v1/compset?id={id}")
    Observable<List<Competitor>> getPropertyCompetitors(@Param("id") Integer id);
}

This code makes request saves data in cache and returns it:

 private Observable<List<Competitor>> callApi(final Integer id) {

    final PropertyCompetitorConnector connector =
            feignConnectorBuilder.buildJacksonConnector(someConnector.class);

    Observable<List<Competitor>> responseObservable = connector.getPropertyCompetitors( id)
            .doOnNext(response -> {
                Compset compset = new Compset();
                if (response != null) {
                    comspet.setCompetitors(response);
                    cacheHandler.put(someCacheName, String.valueOf(id), Compset);
                }
            })
            .doOnError(t -> {
                //error handling
            });

    return responseObservable;
}

How can I do refactor so my service works as same and returns data as same as before even the received json is different?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source