【问题标题】:CXF maven wsdl2java plugin not generating toString() methods in the generated jaxb classesCXF maven wsdl2java 插件未在生成的 jaxb 类中生成 toString() 方法
【发布时间】:2016-08-16 12:25:44
【问题描述】:

我在我的项目中使用了 CXF maven 插件,它将 wsdl 文件转换为适当的 Java 类。但我在生成的 Java 类中看不到 toString() 方法。你能告诉我如何生成 toString() 方法吗?下面是我在项目中使用的 pom.xml 的 sn-p:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.trx</groupId>
  <artifactId>resxWebservices</artifactId>
  <version>10.4.1</version>

  <name>resxWebservices</name>
  <packaging>jar</packaging>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-codegen-plugin</artifactId>
        <version>2.6.0</version>
        <executions>
          <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration>
              <sourceRoot>src</sourceRoot>
              <wsdlOptions>
                <wsdlOption>
                  <wsdl>./resxWebservices.wsdl</wsdl>
                  <extraargs>
                    <extraarg>-xjc-Xts</extraarg>
                  </extraargs>
                </wsdlOption>
              </wsdlOptions>
            </configuration>
            <goals>
              <goal>wsdl2java</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.apache.cxf.xjc-utils</groupId>
            <artifactId>cxf-xjc-runtime</artifactId>
            <version>2.6.0</version>
          </dependency>
          <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
          </dependency>
          <dependency>
            <groupId>org.apache.cxf.xjcplugins</groupId>
            <artifactId>cxf-xjc-ts</artifactId>
            <version>2.6.0</version>
          </dependency>
        </dependencies>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <verbose>true</verbose>
          <fork>true</fork>
          <executable>${JAVA_HOME}/bin/javac</executable>
          <compilerVersion>1.6</compilerVersion>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>org.apache.cxf.xjc-utils</groupId>
      <artifactId>cxf-xjc-runtime</artifactId>
      <version>2.6.0</version>
    </dependency>
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.6</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf.xjcplugins</groupId>
      <artifactId>cxf-xjc-ts</artifactId>
      <version>2.6.0</version>
    </dependency>
  </dependencies>
</project>

【问题讨论】:

    标签: cxf


    【解决方案1】:

    我相信首先你必须让你的 pom 变得杂乱无章。

    您需要有 2 个依赖项才能生成 toString() 方法。你都在用。您正在做的唯一错误是在您的 cxf-codegen-plugin 依赖项中有 cxf-xjc-runtime
    从所述位置删除它。
    并将其放入模块/项目依赖项中。
    它将开始工作。下面是示例 sn-p:

    <dependencies>
        ....
        <dependency>
            <groupId>org.apache.cxf.xjc-utils</groupId>
            <artifactId>cxf-xjc-runtime</artifactId>
            <version>3.0.5</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>3.1.6</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <sourceRoot>${basedir}/src/main/generated</sourceRoot>
                            <wsdlOptions>
                                <wsdlOption>
                                    <wsdl>${basedir}/../someService.wsdl</wsdl>
                                    <extraargs>
                                        <extraarg>-xjc-Xts</extraarg>
                                    </extraargs>
                                </wsdlOption>                               
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.cxf.xjcplugins</groupId>
                        <artifactId>cxf-xjc-ts</artifactId>
                        <version>3.0.5</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    
    </build>
    

    执行此操作后,您将获得如下结果:

    /**
     * Generates a String representation of the contents of this type.
     * This is an extension method, produced by the 'ts' xjc plugin
     * 
     */
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, JAXBToStringStyle.DEFAULT_STYLE);
    }
    

    【讨论】:

    • 非常感谢这个例子是一个与最新库一起工作的例子
    猜你喜欢
    • 2016-02-21
    • 2013-10-26
    • 2012-10-05
    • 2010-11-10
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    相关资源
    最近更新 更多