This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Services and the fly likes About JAXB Databinding in JAX-WS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "About JAXB Databinding in JAX-WS" Watch "About JAXB Databinding in JAX-WS" New topic
Author

About JAXB Databinding in JAX-WS

Dilip Ganesh
Greenhorn

Joined: Sep 30, 2011
Posts: 24
I have developed webservices using CXF framework. We are exposing all our Database operation(Service Layer) methods as a web service methods. We are using Hibernate for persistence.

For example consider I have one Java POJO class(for hibernate mapping) named CountryEntity.java with the following properties(ID,Name,ModifiedDate).

Inside my service layer i have two methods called



Based on JAX-WS documentation i understand that it is using JAXB to do the databinding. When user calling my webservice for creating the new Country record i dont want the user to send the Id value and Modified Date value . So what i done is i annotated the propeties(id,modified date) inside the CountryEntity.java with @XmlTransistent annotation. So that operation is working fine.

But when i call getCountryEntityById web service method its returning CountryEntity object but that object dont have the values for ID and Modified Date property. I understand because of XMLTransistent annotation the values not getting Marshalling.

Can i change my method createCountryEntity taking parameters like (String countryName) , so that i no need to include XMLTransistent annotation in my entity classes.? or Anyother solution for this?


Please help me . Thanks in advance
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
My recommendation is not to expose entities in your web services. If you do, you let middleware technology bleed into your business model which both cause coupling and mixing of concerns.
In a case like yours, I would remove everything related to web service technology from the entity classes and use the "data transfer objects" generated when creating artifacts from the WSDL.
The web service will, upon receiving a request, use the appropriate data in the request to invoke some kind of service that manages the appropriate entity.
If I have not expressed myself clearly, please do not hesitate to ask.
Best wishes!


My free books and tutorials: http://www.slideshare.net/krizsan
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Totally agree with Ivan here.

Imagine the scenario where you need to change your database in some way and hence need to change your data entities. The impact of this change would propagate all the way to clients using your web service. If you had a mapping layer between your data entities and those types exposed on your web service interface, you wouldn't necessarily need to change your interface.

Try to tie down your web service interface early. That way, you will not be breaking client code, even for minor changes made server-side.
Dilip Ganesh
Greenhorn

Joined: Sep 30, 2011
Posts: 24
Ivan Krizsan wrote:Hi!
My recommendation is not to expose entities in your web services. If you do, you let middleware technology bleed into your business model which both cause coupling and mixing of concerns.
In a case like yours, I would remove everything related to web service technology from the entity classes and use the "data transfer objects" generated when creating artifacts from the WSDL.
The web service will, upon receiving a request, use the appropriate data in the request to invoke some kind of service that manages the appropriate entity.
If I have not expressed myself clearly, please do not hesitate to ask.
Best wishes!


Thank you so much for your kind reply. I will read about DTO clearly and i try to use that one in my web service. If i dont understand clearly i will ping you here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About JAXB Databinding in JAX-WS
 
Similar Threads
@WebMethod rules
Generate WSDL at runtime
JAX-WS and undesired exposure of properties
JAX-WS Question
JAX-RPC client for JAX-WS service