The code I'm working on is using jaxb2-maven-plugin
from org.codehaus.mojo
to generate Java classes from XSD schema. I'm looking for a way to automatically implement equals()
and hashCode()
methods for those classes, but it seems there is not a way. I know that there are other JAXB2 Maven plugins that do that (http://confluence.highsource.org/display/J2B/Home for example), but I was wondering if anyone of you encountered this issue before and if there's a way for fixing it. I'm generating the classes using the xjc
goal.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
My answer is for those who can't afford any third part dependency on their generated code.
The org.jvnet.jaxb2_commons:jaxb2-basics plugin adds a lots of
org.jvnet
includes in the generated code and your project will depend onorg.jvnet.jaxb2_commons:jaxb2-basics-runtime
.The org.andromda.thirdparty.jaxb2_commons:commons-lang-plugin plugin generates code which depends on
commons-lang:commons-lang
. This dependency might be easier to bear depending on the situation.I finally found this source code, it might be included in com.sun.xml.bind:jaxb-xjc at some point (version 2.2.4 ?), but until then I can't see any other solution than writing your own plugin.
PS: I can't access confluence.highsource.org, I get a 404. I guess it might have been helpful.
PPS: In my situation, the application I am building is targeted to an environment which has a very limited set of allowed Java libraries (incongruous security restrictions). I was a bit disappointed by the fact that
jaxb2_commons
includes a bunch oforg.jvnet
dependencies in the generated source, just for adding a boringequals
method. I can understand the strategy of jvnet, their tools are very powerful, I may just be trying and using a sledgehammer to crack a nut. Still, I was sorry to note that it was so difficult to find a suitable tool for my situation.JAXB2 Basics you're mentioning is not a property of
maven-jaxb2-plugin
, it is a standalone set of JAXB 2.x plugins you can use with XJC - orjaxb2-maven-plugin
or whatever.What I wanted to ask - why not just use
maven-jaxb2-plugin
? It has so much more functionality compared to the Codehaus plugin - including configuration support for JAXB2 plugins.This is the easiest way to do. Please update version as per your requirements.
You might also want to consider using a fluent builder interface (facilitates the handling of the generated JAXB classes) and a reference to a catalog file in case you are referencing an xmldsig-core-schema (speeds up the generation process, as no remote Schemas from W3C are queried - their servers usually delay the response).
Here is how the catalog file looks like:
And here is the link to the xmldisg-core-schema.xsd: http://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd
Please note that the link is not broken - the W3C server is just delaying the response a few seconds. Can me very cumbersome if that happens during your automated build process, which includes JAXB generation.
I would strongly disagree with using JAXB generated classes as business objects in your code. The classes that are generated by JAXB are beans that are just meant to essentially move element information from the xml file, to the bean's fields. I personally always have my SOAP service convert these generated bean classes to my actual business objects, as XML->Java and vice versa conversion is not black and white all the time. Note that this is my personal opinion, and would love for some others to weigh in on what they do in this situation.
To answer your question though, use a different plug in, or just your use your IDE to make some equals and hashCode methods.
Hope that helps.
EDIT:
I forgot to put my reasoning for this, apologies. Let us say in the next version of your project you want to add some more behavior to your JAXB generated classes, but also want to make some changes to your schema. Now you are regenerating the JAXB generated classes, putting the old behaviors back in, and making your application far more susceptible to bugs in my opinion. The JAXB generated classes are supposed to reflect your XML schema types (and thus your SOAP messages) not your business logic. Hope that makes sense.
There is also a fluent-builder plugin for JAXB that doesn't necessarily generate dependencies to third-party code. Plus, it is a true "Builder" design pattern, whereas the "fluent-api" plugin just adds builder-like methods to the generated classes. It is: https://github.com/mklemm/jaxb2-rich-contract-plugin It also has options to make the generated classes immutable and it can generate methods to copy the state of existing objects into a new builder instance, also supporting "partial" object copy.