users@jaxb.java.net

Re: JAXB newbie questions

From: Kenny MacLeod <kennym_at_kizoom.com>
Date: Tue, 27 Nov 2007 07:52:45 +0000

Aleksei Valikov wrote:
>> 1. JAXB doesn't generate any interfaces only concrete objects. However the
>> docs seem to say that everything should have an interface (and this is what
>> I want).
>
> You may use a customization to do this:
>
> https://jaxb.dev.java.net/guide/Migrating_JAXB_1_0_applications_to_JAXB_2_x.html#Other_Miscellaneous_Notes
>
> Although I like beans better.

I agree. You can take design-to-interface too far, in my opinion, and
while interfaces are a good fit for service classes, I'd say they're
overkill for javabeans.

Note that JAXB1 did generate interfaces and impl classes, and frankly,
it was too heavyweight.

>> 2. The concrete objects JAXB generates aren't very smart, they are JavaBeans
>> only. I would expect a factory or at least constructors that can take all
>> the required parameters plus optional.
>
> These are data objects. Should not be too smart.
> You can use plugins to add features. Many of the plugins are available
> here: https://jaxb2-commons.dev.java.net
> For the task you describe, take
> https://jaxb2-commons.dev.java.net/value-constructor/

The value-constructor plugin does the job (mostly), but it doesn't scale
well at all, in that when your type has many fields (inherited or
declared), the constructor very quickly becomes unwieldy.

I much prefer to use Hanson's fluent-api plugin instead, it results in
much, much readable code than value-constructor.

And also, JAXB does generate an ObjectFactory (or is that a
customisation? I can't remember) than creates instances of your
objects, but all it does is call the constructor and return it, so it's
rather pointless (just there for JAXB1 compatability, I think).



kenny