【问题标题】:Generate toString method with ant用ant生成toString方法
【发布时间】:2015-01-22 11:12:47
【问题描述】:

我有 .wsdl 文件并使用 wsimport 生成类以使用 Web 服务。

问题是我需要在生成的类中添加 toString 方法。我看到在 xjc 中可以,但在 wsimport 中找不到方法。

你知道如何解决这个问题吗?

目前的功能是这样的

    <basename property="basename.file" file="${file}" />
    <mkdir dir="/tmp" />
    <exec executable="wsimport">
        <arg value="-catalog" />
        <arg value="jax-ws-catalog.xml" />
        <arg value="-s" />
        <arg value="${src.generated}" />
        <arg value="-keep" />
        <arg value="-d" />
        <arg value="/temp" />
        <arg value="-Xnocompile" />
        <arg value="file://mock_${basename.file}" />
    </exec>

【问题讨论】:

    标签: ant jaxb jax-ws xjc wsimport


    【解决方案1】:

    根据link

    CXF XJC toString 插件提供了 XJC 插件,用于更新生成的 bean 以实现 toString 方法以覆盖默认的 Object.toString 方法。它使用 Apache Commons Lang ToStringBuilder 类来构建字符串,因此 commons-lang.jar 需要在类路径中可用。

    添加参数 -Xts

    【讨论】:

      【解决方案2】:

      免责声明:我是JAXB2-Basics XJC 插件的强大包的作者。

      您可以为此使用来自JAXB2-BasicsToString。它使用类似于commons-lang 的“战略”方法,但生成无反射代码——这意味着生成的toString() 方法将非常快。

      您将生成如下内容:

      public String toString() {
          final ToStringStrategy strategy = JAXBToStringStrategy.INSTANCE;
          final StringBuilder buffer = new StringBuilder();
          append(null, buffer, strategy);
          return buffer.toString();
      }
      
      public StringBuilder append(ObjectLocator locator, StringBuilder buffer, ToStringStrategy strategy) {
          strategy.appendStart(locator, this, buffer);
          appendFields(locator, buffer, strategy);
          strategy.appendEnd(locator, this, buffer);
          return buffer;
      }
      
      public StringBuilder appendFields(ObjectLocator locator, StringBuilder buffer, ToStringStrategy strategy) {
          {
              USAddress theShipTo;
              theShipTo = this.getShipTo();
              strategy.appendField(locator, this, "shipTo", buffer, theShipTo);
          }
          {
              USAddress theBillTo;
              theBillTo = this.getBillTo();
              strategy.appendField(locator, this, "billTo", buffer, theBillTo);
          }
          // ...
          return buffer;
      }
      

      蚂蚁的用法大致如下:

      <target name="generate-sources">
          <taskdef name="xjc" classname="org.jvnet.jaxb2_commons.xjc.XJC2Task">
              <!-- XJC2 Task classpath -->
              <classpath>
                  <fileset dir="${basedir}/lib">
                      <include name="activation-*.jar"/>
                      <include name="jaxb-api-*.jar"/>
                      <include name="jaxb-impl-*.jar"/>
                      <include name="jaxb-runtime-*.jar"/>
                      <include name="jaxb-core-*.jar"/>
                      <include name="jsr173_api-*.jar"/>
                      <include name="stax-api-*.jar"/>
      
                      <include name="jaxb-xjc-*.jar"/>
                      <include name="jaxb2-basics-ant-*.jar"/>
      
                      <include name="slf4j-*.jar"/>
                      <include name="jcl-over-slf4j-*.jar"/>
                  </fileset>
              </classpath>
          </taskdef>
          <mkdir dir="${basedir}/target/generated-sources/xjc"/>
          <xjc destdir="${basedir}/target/generated-sources/xjc" extension="true">
              <arg line="
                  -Xequals
                  -XhashCode
                  -Xinheritance
                  -XtoString
                  -Xcopyable
                  -XenumValue"/>
              <binding dir="${basedir}/src/main/resources">
                  <include name="**/*.xjb"/>
              </binding>
              <schema dir="${basedir}/src/main/resources">
                  <include name="**/*.xsd"/>
              </schema>
              <!-- Plugins -->
              <classpath>
                  <fileset dir="${basedir}/lib">
                      <include name="jaxb2-basics-plugins-*.jar"/>
                  </fileset>
              </classpath>
          </xjc>
      </target>
      

      distribution 有一个可立即运行的 Ant 示例(请参阅 jaxb2-basics-sample-basic-&lt;VERSION&gt;-ant-src.zip)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-01
        • 2018-04-15
        • 1970-01-01
        • 2012-02-17
        • 2012-06-14
        • 1970-01-01
        • 2021-07-04
        • 2018-08-23
        相关资源
        最近更新 更多