How to decouple missing elements into empty objects using JAXB

I am using the JAXB Maven plugin in Intellij-IDEA (org.codehaus.mojo: jaxb2-maven-plugin: 1.6) to generate a given xsd file for objects. Xsd is quite large and contains many types, most of which are optional.

Right now, when I roll over XML objects to objects, the missing elements are represented as null objects.

So, for example, if I try to do rootObject.getAccount().getAccountName()

, but the account element is missing from the XML at all, I getNullPointerException

I would like to customize JAXB to showcase the complete object tree to include those that represent the missing elements.

Is it possible?

+3


source to share


1 answer


This would actually be incorrect, since the property is null

semantically different from this property, which is suppressed by some default. The installation of some property new Account()

in account

for missing value will be incorrect.

If you want to simplify your code, consider using the JAXB2 Fluent API plugin . You will probably get the code:

rootObject.withAccount().getAccountName()

      

(Implicitly initializes account

.) I'm not 100% sure if it will work this way, but definitely worth a try.

Update

A few notes on your comments.

I looked at the link you posted and tried to find it on the internet, but if I understand correctly, it is mainly used to fill in values ​​in objects, not read them. You posted the link: FluentApi plugin provides Chaining method for bean setters. I was looking for a way to somehow avoid checking for nulls when trying to access nested classes.

You're right. However, you said that "undo the full tree of objects to include those that represent the missing items." My point is that it would be wrong for JAXB to populate these missing properties, but you can do that, and the Fluent API plugin is a closure I would know to an easy to use API for this case. But that doesn't answer your question as there may be no answer at the moment.

The comments there basically suggest either sucking it in, checking for null values ​​along the way, using reflection, or just catching the error. I ended up with the first option for performance reasons.



Probably the best thing you can do at the moment. This is what I do (checking null

s) all the time. Well, not exactly pleasant, but also pretty trivial.

Then, although I understand a little, how can this be solved at all?

XJC supports plugins that can enhance the generated code. This is a very powerful tool, I've done a lot with these plugins. But what kind of code should be generated? How should this look like in Java?

The default fill properties are simply wrong. I think this will not lead to good results.

Something like rootObject.getAccount().getAccountName()

won't work because of null

s. So how would this look like in Java?

The best thing that came to my mind is an interface like getValueByPath(String propertyPath)

so you can do something like:

rootObject.getValueByPath(`account.accountName`);

      

getValueByPath(String path)

can be generated by a plugin. You can write code to resolve this path

without any reflection. The generated code will check for its own properties and either return the target value, call getValueByPath(...)

on it, or return null

if not found / null

.

But then you won't have autocomplete and the whole thing will be much more error prone.

+2


source







All Articles