How to generate equals() and hashcode() methods us

2019-02-13 20:48发布

问题:

The generated classes from my WSDL using wsimport are not having equals() and hashcode() methods. How can I customize and generate the client classes to get equals() and hashcode() methods.

I am not sure about using JAXB to achieve this.

In Axis2.0 generated stubs these methods are generated but not sure why such a basic thing is not available in JAXWS!

回答1:

You can use the JAXB2 Basics Plugin to generate equals() and hashcode() methods:

  • https://github.com/highsource/jaxb2-basics
  • http://confluence.highsource.org/display/J2B/JAXB2+Basics+Plugins


回答2:

If you are looking to generate hashcode() and equals() using wsimport in maven, check this answer on how to generate value constructors, but also includes the configuration for generating hashcode() and equals() too:

How do I make wsimport generate constructors?



回答3:

More information on how it worked. I have to add classpath to jaxb2-commons and without which wsimport runs without complaining but nothing happens! After adding the classpath as below

<path id="jaxb2-commons.classpath">
    <fileset dir="${dir.toolchain}/noarch/jaxb2-basics-dist-0.6.0">
        <include name="**/*.jar" />
    </fileset>
</path>

the below wsimport worked as expected

<wsimport wsdl="@{dir-wsdl}/@{name-wsdl}"
          taskname="wsimport-@{service}"
          destdir="@{dest-dir}"
          sourcedestdir="@{source-dest-dir}"
          package="@{package}"
          keep="@{keep}"
          verbose="@{verbose}"
          xdebug="@{xdebug}"
          xnocompile="@{xnocompile}"
          target="2.1">
    <binding dir="@{dir-wsdl}" includes="bindings-wsdl-@{name-wsdl}.xml, bindings-schema-@{name-wsdl}.xml" />
    <xjcArg value="-Xequals" />
    <xjcArg value="-XhashCode" />
    <xjcArg value="-XtoString" />
    <!-- Generates per-package jaxb.index file which lists all of the schema-derived classes in this package.-->
    <xjcArg value="-Xjaxbindex" />
    <xjcArg value="-Xsetters" />
</wsimport>


标签: jaxb jax-ws