2012-06-21 26 views
8

wsimport生成沒有參數化構造函數的源代碼。因此,如果Bean有許多屬性,需要手動調用所有的setter方法:如何讓wsimport生成構造函數?

Person person = new Person(); 
person.setName("Alex"); 

Address address = new Address(); 
address.setCity("Rome"); 

person.setAddress(address); 

它更具有可讀性,方便只寫這樣的代碼:

Person person = new Person("Alex", new Address("Rome")) 

那麼,有沒有任何方式使wsimport做這份工作? (我正在使用maven wsimport插件)

回答

5

使用JAXB Value Constructor Plugin作爲xjc工具。 你可以maven-xjc-plugin像這樣使用它:

<project> 
    ... 
    <build> 
    ... 
    <plugins> 
     ... 
     <plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>xjc-maven-plugin</artifactId> 
     <version>1.0-beta-2-SNAPSHOT</version> 
     <executions> 
      <execution> 
      <goals> 
       <goal>xjc</goal> 
      </goals> 
      <configuration> 
       <task><![CDATA[ 
       <xjc schema="src/main/resources/com/acme/services.xsd" package="com.acme"> 
        <arg value="-Xvalue-constructor" /> 
       </xjc> 
       ]]></task> 
      </configuration> 
      </execution> 
     </executions> 
     </plugin> 
     ... 
    </plugins> 
    ... 
    </build> 
    ... 
</project>  
+0

你能告訴我在哪裏可以找到這個神器的存儲庫嗎?謝謝。 – Alex

+0

它在[maven cetral](http://search.maven.org/#artifactdetails|org.jvnet.jaxb2_commons|jaxb2-value-constructor|3.0|jar) – npe

+0

任何想法如何從命令行執行此操作? – pathikrit

0

wsimport使用xjc創建Java類。它支持插件,其中一些插件可以在jaxb2-commons找到。還有一個構造函數插件,它爲所有子元素創建一個具有參數的構造函數。

jax-ws-commons頁面提供了有關如何在JAX-WS Maven插件中使用XJC插件的說明。

8

要使用的wsimport與XJC做到這一點:

<plugin> 
      <groupId>org.jvnet.jax-ws-commons</groupId> 
      <artifactId>jaxws-maven-plugin</artifactId> 
      <version>2.3</version> 

      <dependencies> 
       <!-- put xjc-plugins on the jaxws-maven-plugin's classpath --> 
       <dependency> 
        <groupId>org.jvnet.jaxb2_commons</groupId> 
        <artifactId>jaxb2-basics</artifactId> 
        <version>0.6.4</version> 
       </dependency> 
       <dependency> 
        <groupId>org.jvnet.jaxb2_commons</groupId> 
        <artifactId>jaxb2-value-constructor</artifactId> 
        <version>3.0</version> 
       </dependency> 
      </dependencies> 
      <executions> 
          <execution> 
        <id>wsdl-gen</id> 
        <goals> 
         <goal>wsimport</goal> 
        </goals> 
        <configuration> 
         <wsdlDirectory>${project.basedir}/src/main/resources/wsdl/</wsdlDirectory> 
         <bindingDirectory>${project.basedir}/src/main/resources/wsdl</bindingDirectory> 
         <sourceDestDir>${project.build.directory}/generated-sources/wsimport</sourceDestDir> 
         <extension>true</extension> 
         <target>2.2</target> 
         <verbose>true</verbose> 
         <!-- tell JAXB to actually use xjc-plugins --> 
         <args> 
          <arg>-B-Xequals</arg> 
          <arg>-B-XhashCode</arg> 
          <arg>-B-Xvalue-constructor</arg> 
         </args> 
        </configuration> 
       </execution> 
    </executions> 
     </plugin> 

的關鍵部分是將通過-X ...值在-B。

...

<args> 
     <arg>-B-Xequals</arg> 
     <arg>-B-XhashCode</arg> 
     <arg>-B-Xvalue-constructor</arg> 
    </args> 

...

這生成一個值構造器,equals和hashCode方法。 equals和hashcode由jaxb2-basics插件提供。

+1

任何想法如何從命令行執行此操作?就像我沒有一個maven項目,但只想wsimport .. – pathikrit