javaCurious
javaCurious

Reputation: 140

JAXB default value for null elements when marshalling

At the time of marshalling of JAXB object I want to set some defult value to the resulting XML.

I do not want to use nillable=true as it generates empty tag with unnecessary xsi:nil="true", and this is not for setting default value. Instead I want to generate the XML with some placeholder characters such as '?'.

Use case : I am going to build a tool for WebService testing. There I need to present the entire request xml to the user (Like SOAPUI).

Upvotes: 3

Views: 1714

Answers (1)

bdoughan
bdoughan

Reputation: 149047

Use case : I am going to build a tool for WebService testing. There I need to present the entire request xml to the user (Like SOAPUI).

The idea of the place holder character isn't really going to work. For example ? is an ok default value for a string, but not an int, boolean, or for most complex values (i.e. representing the nested address information for a customer). Instead you will want a value that reflects the type.

Then I would have to write large and complex reflection based code. Just assume that that is almost not possible in my case.

This reflection code probably won't be as bad as you are imagining. A quick search will probably also reveal libraries that populate objects with "dummy" data. When hooking it up with JAXB you could leverage a Marshaller.Listener to populate the object on the before marshal event.

Upvotes: 1

Related Questions