cxf-codegen-plugin
Posted by Bruce Tsai
透過 maven plugin 與 cxf plugin 自動產生 web service 的 client code。配置設定檔如下,設定值參考官方說明。
maven 配置
- pom.xml
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.1.6</version>
<executions>
<execution>
<id>HelloWorldService.wsdl</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${basedir}/src/main/java</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/HelloWorldService.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
<execution>
<id>HelloCXFService.wsdl</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${basedir}/src/main/java</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/HelloCXFService.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-xjc-ts</artifactId>
<version>2.2.9</version>
</dependency>
</dependencies>
</plugin>
關於 JAXBElement 的處理
在不同的 library 條件下產出的原始碼會有些不同。部份產出的原始碼中,物件屬性會被轉換為 JAXBElement 型別,造成在實際開發上的不便,可透過 binding 的配置將屬性轉換為原本的型別。
- 先加入 binding 配置文件 (bindings.xml)
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<jaxb:bindings>
<!-- 避免產生 JAXBElement 的型別屬性 -->
<jaxb:globalBindings generateElementProperty="false" />
</jaxb:bindings>
</jaxb:bindings>
- 修改 executing 中設定
<execution>
<id>HelloWorldService.wsdl</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${basedir}/src/main/java</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/HelloWorldService.wsdl</wsdl>
<bindingFiles>
<!-- 自定義 binding 配置 -->
<bindingFile>${basedir}/src/main/resources/bindings.xml</bindingFile>
</bindingFiles>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
結果
- 產生 JAXBElement 型別屬性的物件
package com.fet.crm.sample.common.model.xsd;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Category complex type 的 Java 類別.
*
* <p>下列綱要片段會指定此類別中包含的預期內容.
*
* <pre>
* <complexType name="Category">
* <complexContent>
* <extension base="{http://model.common.sample.crm.fet.com/xsd}Base">
* <sequence>
* <element name="id" type="{http://www.w3.org/2001/XMLSchema}int" minOccurs="0"/>
* <element name="name" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* </extension>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Category", propOrder = {
"id",
"name"
})
public class Category
extends Base
{
protected Integer id;
@XmlElementRef(name = "name", namespace = "http://model.common.sample.crm.fet.com/xsd", type = JAXBElement.class, required = false)
protected JAXBElement<String> name;
/**
* 取得 id 特性的值.
*
* @return
* possible object is
* {@link Integer }
*
*/
public Integer getId() {
return id;
}
/**
* 設定 id 特性的值.
*
* @param value
* allowed object is
* {@link Integer }
*
*/
public void setId(Integer value) {
this.id = value;
}
/**
* 取得 name 特性的值.
*
* @return
* possible object is
* {@link JAXBElement }{@code <}{@link String }{@code >}
*
*/
public JAXBElement<String> getName() {
return name;
}
/**
* 設定 name 特性的值.
*
* @param value
* allowed object is
* {@link JAXBElement }{@code <}{@link String }{@code >}
*
*/
public void setName(JAXBElement<String> value) {
this.name = value;
}
}
- 產生不使用 JAXBElement 型別屬性的物件
package com.fet.crm.sample.common.model.xsd;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Category complex type 的 Java 類別.
*
* <p>下列綱要片段會指定此類別中包含的預期內容.
*
* <pre>
* <complexType name="Category">
* <complexContent>
* <extension base="{http://model.common.sample.crm.fet.com/xsd}Base">
* <sequence>
* <element name="id" type="{http://www.w3.org/2001/XMLSchema}int" minOccurs="0"/>
* <element name="name" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* </extension>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Category", propOrder = {
"id",
"name"
})
public class Category
extends Base
{
protected Integer id;
@XmlElement(nillable = true)
protected String name;
/**
* 取得 id 特性的值.
*
* @return
* possible object is
* {@link Integer }
*
*/
public Integer getId() {
return id;
}
/**
* 設定 id 特性的值.
*
* @param value
* allowed object is
* {@link Integer }
*
*/
public void setId(Integer value) {
this.id = value;
}
/**
* 取得 name 特性的值.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* 設定 name 特性的值.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
}