I am working on a service has to forward the incoming requests to a destination service after performing some alterations on the object received in the request body and return the response received from the service. There are only two types of objects (A or B) expected in the response body for which I want to use a wildcard instead of Object class to keep the code cleaner.
Now I know that we can not use generics with two classes such that the generic class object must belong to either of the two classes.
public ResponseEntity<?> getDataFromService(C requestBody) {
C updatedRequestBody = performAlterationsOnRequestBody (requestBody);
ResponseEntity<?> responseObject = forwardToRespectiveService(updatedRequestBody);
// responseObject body can contain object of either class A or class B.
return response;
}
Is the approach to use a wildcard correct way to go about?