I am recently working on JAX-WS (a newbie, interested to learn web services), I have a design concern which would be a simple hurdle to overcome but i am not finding the solution anywhere.
okay, consider if i have to use web services in an organization to fetch details about an employee... i write a web service which fetches the details and provides me Employee details
At the service consumer i use wsimport to generate the classes so i would get an Employee object from the service (i am very happy about it) which makes my job dead easy!
But consider the service client and the web service both are within the organization and they have a "domain model" which is same and shared!!! i.e., the same Employee object (and all other objects employee composes) used by the web service is used by the web service client as well
but since i use wsimport, the Employee class generated by wsimport is actually a different class which forces me to transform the wsimport generated Employee object to the Employee object present in my domain model
Is there any way i can avoid wsimport from generating stub and use the Classes i already have? this way i can avoid unnecessary transformations!
Assuming that you are using JAXB for marshalling and unmarshalling; have you considered annotating the existing model classes, such as Employee, using JAXB annotations?
In such a case, to make your job easier, you can first generate JAXB beans (and other artifacts) from the WSDL using wsimport and then look at the annotations in the JAXB bean classes that correspond to your model classes.
Later, when using wsimport to generate, for instance, client artifacts, you will have to use an external JAXB binding file (using the -b option) in order to persuade wsimport to use your custom JAXB bean classes.
Personally, I think I would prefer the following approach:
- Create one or more XML schemas describing the model objects.
- Use the JAXB schema compiler (XJC - a plugin is available for Eclipse and IntelliJ) to generate model classes from the XML schema(s).
In this approach, the XML schemas describing the model objects is the "master" from which the model classes are created.
posted 10 years ago
Thanks a lot Ivan... i think the first option (annotations) sounds good to me.. the reason being that i dont want my domain model classes to be generated from an XSD, as it can keep evolving.
I actually feel little reluctant to annotate the domain classes with JAXB annotations but considering that only few classes (those which are transmitted from service to service consumer and vice versa) will be annotated, this option would look neat.
Let me give it a try...
posted 10 years ago
John Pradeep.v wrote:
I actually feel little reluctant to annotate the domain classes with JAXB annotations
I agree with you. An option would be to create special transfer objects that are separated from the domain classes. Then you perform some kind of "translation" in the interaction layer of the web service, perhaps implementing code to populate a transfer object from a corresponding domain class instance in the appropriate transfer object class. This would also allow you to create larger transfer objects that consist of multiple domain class instances etc.
Your question and the associated problems, for instance versioning of the data model(s) used by web services, are very interesting, but not easy to solve.
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad: