Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on JAXB example

 
Kamal Tripathi
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Kamal Tripathi
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic