I have bunch of REST style web services which support XML and JSON type responses. Now I have to modify existing web services such that they provide Protobuf type of responses.
Developer who has developed these services has created three layers. Service
layer, DAO
layer and Processing
layer.
Service layer calls one of the method of processing layer and processing layer calls DAO methods and returns either JSON or XML type response (POJO) to the service. Service layer then sends back this POJO to client.
For example, if service to add list of items
is called then that code roughly looks as shown below:
@Path("/item")
class ShoppingItemService {
// annotations
public ItemAddResponse addList(/* parameters required for this method */) {
ItemProcessor processor = new ItemProcessor();
ItemAddResponse response = processor.process(/* pass some data to processing layer */);
return Response.ok().format("XML").build(response);
}
}
Here ItemAddResponse
is already annotated with JAXB
type annotations for XML
response and other annotations for JSON
response.
I cannot reuse same ItemAddResponse
because Protobuf generates its own classes using schema files.
Processor class has all the business logic and If I want to add support for Protobuf then I have create new method in processor and copy-paste almost all the business logic, but just return object of protobuf generated class.
This approach is time consuming and there atleast 50+ similar methods where I have to do copy-paste work. I want to avoid this and would like to design some elegant solution.
I would like to know how can I add support for Protobuf with minimal amount of changes?
I am reading Head First Design patterns
book but this book is really huge and it will take lot of time to find proper design pattern.
Is there any design pattern which can be useful in this type of situations?
Thanks,
Chandrashekhar