JAXB Zero-Field Marshalling

This is a fairly simple request, but I just did not find a way to do this.

I am basically trying to set up a role in JAXB, which says that whenever a null field is encountered, instead of ignoring it in the output, set it to an empty value. So for the class:

@XMLRootElement Class Foo { Integer num; Date date; …. } 

When it was sent to the XML file, if the date field is null, my output does not contain this element. What I want to do is include all the fields in the output; and if they are zero, replace them with, say, a space. Thus, the output should be:

 <foo> <num>123</num> <date></date> </foo> 

Thank,

Jalpesh.

+45
java marshalling jaxb
May 13, '09 at
source share
4 answers

Thank you guys for your answers.

Chris Dale - I tried your approach, and he really did not do what I wanted. JAXB still ignored my null values, despite defining a default value for my fields.

I came across an answer after one of the forums in Jersey pointed me to the documentation section 2.2.12.8 No value .

Basically, all I had to do was add the following to my fields:

 @XmlElement(nillable = true) 

As I added, JAXB will display these fields when they are sorted by XML as follows:

 ... <num>5</num> <date xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> .... 
+38
May 13, '09 at 22:55
source share

But but but ... an empty string is not a valid lexical representation for a date, so you cannot do this. that is, if you created an XML document with an empty value for the date field, it will not be validated correctly.

In other words, if your date element has minOccurs of 1 or more, and not nillable , then you must have (1 or more) dates that cannot be empty (or spaces, or other minor ones).

+4
May 13 '09 at 3:28 p.m.
source share

As indicated in another answer, it is invalid because it is not a valid date. I had a similar problem that I wanted to deal with (especially). Since you cannot use null, you can use the default mechanism in JAXB. The following value will be set by default if none are specified. You can detect this special date through the code and handle this exception case.

 @XmlElement(defaultValue="1970-01-01T00:00:00.0-00:00") 

This way you can define and remove the date value, but you just cannot use null for this.

+1
May 13, '09 at 16:45
source share

In MOXy, you can specify how jsonProvider should do its job for JAXB.

So, when you do JAX-RS, add the following code to your class obtained from the application

I used this code on Tomcat 7 with good results. (eclipselink 2.4.1)

 @ApplicationPath("/rest") public class RestApplication extends Application { ... public Set< Object> getSingletons() { HashSet<Object> set = new HashSet<Object>(1); set.add( newMoxyJsonProvider()); return set; } public static MOXyJsonProvider newMoxyJsonProvider() { MOXyJsonProvider result = new MOXyJsonProvider(); //result.setAttributePrefix("@"); result.setFormattedOutput( false); result.setIncludeRoot( false); result.setMarshalEmptyCollections( true); //result.setValueWrapper("$"); return result; } 

In Glassfish 3.1.2 and WAS 8.5, however, newMoxyJsonProvider () is not required, but then the JAXB provider is configured by the server. In the case of Glassfish, which comes with MOXy, I saw the same problems with null values. I have not tested it yet, but guess that the answer is to configure JAXB at the application server level, if at all possible.

+1
Mar 05 '13 at 10:52
source share



All Articles