aspose file tools*
The moose likes Web Services Certification (SCDJWS/OCEJWSD) and the fly likes Doubt on JAXB example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Services Certification (SCDJWS/OCEJWSD)
Bookmark "Doubt on JAXB example" Watch "Doubt on JAXB example" New topic
Author

Doubt on JAXB example

Kamal Tripathi
Ranch Hand

Joined: Oct 02, 2008
Posts: 86
Source: SOA Using Java Web Services (Mark Hansen)

Objective: Example exercise to show the use of @XmlJavaTypeAdapter for the mapping of below schema to Address.java using an intermediate AddressXML class. AddressAdapter is the adapter class which converts from Address to AddressXML. I have 2 doubts here. Doubts are towards teh end.











My two doubts.

1) in AddressAdapter how do lines 40 and 69 even work?? Assiging int to Integer field and vice versa. I ran the sample code but it is generating an optimized class file. So I am not sure if it is correct. The book too mentions this sample code and I am copy pasting from the sample code available on the website.

2) AddressXML doesn't define any adapter for phone field. Adapter is mentioned only in the class definition of Phone.java. Is it equivalent?? Can we define declare adapter in field as well as the class ? I am guessing the answer is yes but not sure. I think it has been done so since both Address and AddressXML use Phone class.

Sorry for pasting such huge code. But I already trimmed it for posting and haven't posted all the classes.

P.S. Those who want to download the code can do so from the official website: http://soabook.com/code.html.
If you want to take a look at the example in the downloaded code, it is at chap05/xmladapter/src/java/samples. It may be tough to glean from the code the objective of that example so posted the objective at the top.

Kamal Tripathi
SCJP 1.4 90%, SCWCD5 94%, Next SCDJWS--> In Naescent stage. Researching abt exam and material itself.
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
Kamal Tripathi wrote:
1) in AddressAdapter how do lines 40 and 69 even work?? Assiging int to Integer field and vice versa. I ran the sample code but it is generating an optimized class file. So I am not sure if it is correct. The book too mentions this sample code and I am copy pasting from the sample code available on the website.

This is called autoboxing and unboxing and facilitates automatic converstion between primitives, like int, float etc, and their counterpart wrapper class, like Integer, Float etc.
Reference: http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html

2) AddressXML doesn't define any adapter for phone field. Adapter is mentioned only in the class definition of Phone.java. Is it equivalent?? Can we define declare adapter in field as well as the class ? I am guessing the answer is yes but not sure. I think it has been done so since both Address and AddressXML use Phone class.

Each class has an adapter of its own, responsible for marshalling the corresponding class. If there is a non-primitive field, like phone in the address class, then the responsible for marshalling and unmarshalling it should be delegated to the adapter for the type of the field.
This way coupling is reduced and cohesion increased.
Hope this makes things more clear.
Best wishes!


My free books and tutorials: http://www.slideshare.net/krizsan
Kamal Tripathi
Ranch Hand

Joined: Oct 02, 2008
Posts: 86
Yes I guess first one should have been a no-brainer for me, guess I am missing simple details in the midst of this deluge of details on WS .

Thanks for the second explanation.
 
Consider Paul's rocket mass heater.
 
subject: Doubt on JAXB example