views:

371

answers:

1

Hi guys,

First attempt to use this cool site - after searching for 2 hours: So I have a Java Bean that is given (I can only annotate not change) and need to map it to XML using JAXB. I would like primitives types not to be printed when they contain their language default, or a user-defined default.

As said I cannot change the java bean, and therefore change the primitive types into their Object Wrappers counterparts.

How do you do that best?

Sample bean:

class Foo {  
public String name;  
// -1 is user defined default, to indicate field is not set.  
public long someIdx=-1;  
// ...  
}  

Foo f = new Foo();  
f.name = "Duke";

for this instantiation, what I would like is the following output:

<foo><name>Duke</name></foo>
A: 

You could use JAXB's XmlAdapters. Availible since JAXB 2.0 (JDK 6.0)

Using the adapter, you let the bean alone and annotate/change a completely different class. This should be not a problem in your case.

See my reply here

See JAXB's author's blog post

ivan_ivanovich_ivanoff
Thanks, I acutally now just wrote converters encapsulating the types that was the easiest way.