I am trying to refactor some code in order to reduce code duplication.
Currently I have a service that takes its own custom Payload class and returns its own Response class. And currently it is not one service class that does this but around 5 or 6. So that makes for a LOT of code duplication even though the body of the methods are literally the same, just the return type is different.
For example:
public class ServiceA {
ResponseA getResponse(PayloadA){...}
}
public class ServiceB {
ResponseB getResponse(PayloadB){...}
}
public class ServiceC {
ResponseC getResponse(PayloadC){...}
}
Where the method bodies are literally word for word the same but because it is a post request using WebClient where the methods end with ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(response, new TypeReference<>() {});
, we are just copying the method again and again.
I tried to implement an abstract superclass called RestService and have two empty interfaces called RestResponse and RestPayload that each service can implement on their own but then how do I get the service class to return it’s own Response type and not the RestResponse type which is just an empty interface?