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

    <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> 

请您参考如下方法:

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

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

<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); 
} 


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!