使用Maven根據WSDL生成生成Java代碼

轉載:https://blog.csdn.net/pzasdq/article/details/52601473java

爲便於本身學習,整理web

修改pom.xmlspring

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3   <modelVersion>4.0.0</modelVersion>
 4   <groupId>org.ygy.cxf</groupId>
 5   <artifactId>cxf-date</artifactId>
 6   <packaging>war</packaging>
 7   <version>0.0.1-SNAPSHOT</version>
 8   <name>cxf-date Maven Webapp</name>
 9   <url>http://maven.apache.org</url>
10   
11   <!-- 屬性配置 -->
12     <properties>
13         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14         <cxf.version>2.7.5</cxf.version>
15         <spring.version>3.1.1.RELEASE</spring.version>
16     </properties>
17 
18     <dependencies>
19         <dependency>
20             <groupId>junit</groupId>
21             <artifactId>junit</artifactId>
22             <version>4.10</version>
23             <scope>test</scope>
24         </dependency>
25 
26         
27         <dependency>
28             <groupId>org.apache.cxf</groupId>
29             <artifactId>cxf-rt-frontend-jaxws</artifactId>
30             <version>${cxf.version}</version>
31         </dependency>
32 
33         <dependency>
34             <groupId>org.apache.cxf</groupId>
35             <artifactId>cxf-rt-transports-http</artifactId>
36             <version>${cxf.version}</version>
37         </dependency>
38 
39         <dependency>
40             <groupId>org.apache.cxf</groupId>
41             <artifactId>cxf-rt-transports-http-jetty</artifactId>
42             <version>${cxf.version}</version>
43         </dependency>
44 
45         <dependency>
46             <groupId>org.springframework</groupId>
47             <artifactId>spring-context</artifactId>
48             <version>${spring.version}</version>
49         </dependency>
50 
51         <dependency>
52             <groupId>org.springframework</groupId>
53             <artifactId>spring-web</artifactId>
54             <version>${spring.version}</version>
55         </dependency>
56 
57         <dependency>
58             <groupId>org.eclipse.jetty</groupId>
59             <artifactId>jetty-webapp</artifactId>
60             <version>8.1.11.v20130520</version>
61         </dependency>
62 
63     </dependencies>
64 
65     <build>
66         <finalName>cxf-date</finalName>
67         
68         <plugins>
69             <plugin>
70                 <groupId>org.apache.cxf</groupId>
71                 <artifactId>cxf-codegen-plugin</artifactId>
72                 <version>${cxf.version}</version>
73                 <executions>
74                     <execution>
75                         <id>generate-sources</id>
76                         <phase>generate-sources</phase>
77                         <configuration>
78                             <sourceRoot>src/main/resources/cxf</sourceRoot>
79                             <wsdlOptions>
80                                 <wsdlOption>
81                                     <wsdl>http://localhost:8080/cxf-date/services/BookService?wsdl</wsdl>
82                                     <frontEnd>jaxws21</frontEnd>
83                                     <faultSerialVersionUID>1</faultSerialVersionUID>
84                                 </wsdlOption>
85                             </wsdlOptions>
86                         </configuration>
87                         <goals>
88                             <goal>wsdl2java</goal>
89                         </goals>
90                     </execution>
91                 </executions>
92             </plugin>
93         </plugins>
94     </build>
95 </project>
View Code

用cxf的wsdl2java工具生成客戶端代碼,可是發現生成的全部自定義的dto類的成員變量,都被映射成JAXBElement<T>的類型了,用起來很麻煩。apache

解決辦法:架構

法一:後來通過試驗,發現將wsdl裏的nillable修改成false後,再產生的java代碼就是簡單的POJO了。(此方法生成成功的須要的代碼,但不肯定是否有弊端)app

百科一下---------nillable:frontend

一個指示符,指示是否能夠將顯式的零值分配給該元素。此項應用於元素內容而且不是該元素的屬性。默認值爲 false。eclipse

若是 nillable 爲 true,將使該元素的實例能夠將 nil 屬性設置爲 true。nil 屬性被定義爲實例的 XML 架構命名空間的一部分。webapp

例如,下段定義了單個元素,同時將 nillable 設置爲 true。maven

<xs:element name="myDate" type="xs:date" nillable="true"/>

法二:換一種調用方法(嫌麻煩,何嘗試):

User user = new User();    
ObjectFactory objFac=new ObjectFactory();    
JAXBElement<String> name = objFac.createUserUsername("smallnest");    
user.setUsername(name); 

方法三:修改pom文件,添加-b參數,並添加自定義binding文件(由於不熟悉,沒有測試生成成功)

<extraargs>
    <extraarg>-autoNameResolution</extraarg>
    <extraarg>-b</extraarg>
    <extraarg>my_binding.xml</extraarg>  
</extraargs>

my_binding.xml以下:

<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
    <jaxb:bindings>
        <jaxb:globalBindings generateElementProperty="false" />
    </jaxb:bindings>
</jaxb:bindings>
相關文章
相關標籤/搜索