cxf客户端代码生成

This commit is contained in:
Xiong Neng 2018-06-19 14:57:48 +08:00
parent 26e4227ace
commit be5ac402cc
11 changed files with 668 additions and 6 deletions

View File

@ -6,20 +6,44 @@
## 客户端动态代理调用 ## 客户端动态代理调用
这个在单元测试类ApplicationTests中有演示 这个在单元测试类ApplicationTests中有演示这里要注意的是model类的包名一定要放到指定的路径下。
也就是targetNamespace的倒叙包名中。
## 生产客户端代码 ## 客户端代码生成
apache的wsdl2java工具使用`-autoNameResolution`自动处理 有两种方式生成客户端调用代码
**Apache的wsdl2java工具**
``` ```
wsdl2java -autoNameResolution http://xxx?wsdl wsdl2java -autoNameResolution http://xxx?wsdl
``` ```
JDK自带的工具 **JDK自带的工具(推荐)**
``` ```
wsimport -p com.enzhico.land.client -keep http://xxx?wsdl -s d:/ws -B-XautoNameResolution wsimport -encoding utf-8 -p com.xncoding.webservice.client -keep http://xxx?wsdl -s d:/ws -B-XautoNameResolution
```
其中:
```
-encoding 指定编码格式此处是utf-8的指定格式
-keep是否生成Java源文件
-s指定.java文件的输出目录
-d指定.class文件的输出目录
-p定义生成类的包名不定义的话有默认包名
-verbose在控制台显示输出信息
-b指定jaxws/jaxb绑定文件或额外的schemas
-extension使用扩展来支持SOAP1.2
```
生成的客户端代码不能改包名
``` java
CommonService_Service c = new CommonService_Service();
com.xncoding.webservice.client.User user = c.getCommonServiceImplPort().getUser("Tom");
assertThat(user.getName(), is("Tom"));
``` ```
## 许可证 ## 许可证

View File

@ -0,0 +1,54 @@
package com.xncoding.webservice.client;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.9-b130926.1035
* Generated source version: 2.2
*
*/
@WebService(name = "CommonService", targetNamespace = "http://model.webservice.xncoding.com/")
@XmlSeeAlso({
ObjectFactory.class
})
public interface CommonService {
/**
*
* @param userName
* @return
* returns com.xncoding.webservice.client.User
*/
@WebMethod
@WebResult(targetNamespace = "")
@RequestWrapper(localName = "getUser", targetNamespace = "http://model.webservice.xncoding.com/", className = "com.xncoding.webservice.client.GetUser")
@ResponseWrapper(localName = "getUserResponse", targetNamespace = "http://model.webservice.xncoding.com/", className = "com.xncoding.webservice.client.GetUserResponse")
public User getUser(
@WebParam(name = "userName", targetNamespace = "")
String userName);
/**
*
* @param userName
* @return
* returns java.lang.String
*/
@WebMethod
@WebResult(targetNamespace = "")
@RequestWrapper(localName = "sayHello", targetNamespace = "http://model.webservice.xncoding.com/", className = "com.xncoding.webservice.client.SayHello")
@ResponseWrapper(localName = "sayHelloResponse", targetNamespace = "http://model.webservice.xncoding.com/", className = "com.xncoding.webservice.client.SayHelloResponse")
public String sayHello(
@WebParam(name = "userName", targetNamespace = "")
String userName);
}

View File

@ -0,0 +1,94 @@
package com.xncoding.webservice.client;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.9-b130926.1035
* Generated source version: 2.2
*
*/
@WebServiceClient(name = "CommonService", targetNamespace = "http://model.webservice.xncoding.com/", wsdlLocation = "http://localhost:8092/services/CommonService?wsdl")
public class CommonService_Service
extends Service
{
private final static URL COMMONSERVICE_WSDL_LOCATION;
private final static WebServiceException COMMONSERVICE_EXCEPTION;
private final static QName COMMONSERVICE_QNAME = new QName("http://model.webservice.xncoding.com/", "CommonService");
static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("http://localhost:8092/services/CommonService?wsdl");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
COMMONSERVICE_WSDL_LOCATION = url;
COMMONSERVICE_EXCEPTION = e;
}
public CommonService_Service() {
super(__getWsdlLocation(), COMMONSERVICE_QNAME);
}
public CommonService_Service(WebServiceFeature... features) {
super(__getWsdlLocation(), COMMONSERVICE_QNAME, features);
}
public CommonService_Service(URL wsdlLocation) {
super(wsdlLocation, COMMONSERVICE_QNAME);
}
public CommonService_Service(URL wsdlLocation, WebServiceFeature... features) {
super(wsdlLocation, COMMONSERVICE_QNAME, features);
}
public CommonService_Service(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public CommonService_Service(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* @return
* returns CommonService
*/
@WebEndpoint(name = "CommonServiceImplPort")
public CommonService getCommonServiceImplPort() {
return super.getPort(new QName("http://model.webservice.xncoding.com/", "CommonServiceImplPort"), CommonService.class);
}
/**
*
* @param features
* A list of {@link WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* @return
* returns CommonService
*/
@WebEndpoint(name = "CommonServiceImplPort")
public CommonService getCommonServiceImplPort(WebServiceFeature... features) {
return super.getPort(new QName("http://model.webservice.xncoding.com/", "CommonServiceImplPort"), CommonService.class, features);
}
private static URL __getWsdlLocation() {
if (COMMONSERVICE_EXCEPTION!= null) {
throw COMMONSERVICE_EXCEPTION;
}
return COMMONSERVICE_WSDL_LOCATION;
}
}

View File

@ -0,0 +1,60 @@
package com.xncoding.webservice.client;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>getUser complex type的 Java
*
* <p>以下模式片段指定包含在此类中的预期内容
*
* <pre>
* &lt;complexType name="getUser">
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element name="userName" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* &lt;/sequence>
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getUser", propOrder = {
"userName"
})
public class GetUser {
protected String userName;
/**
* 获取userName属性的值
*
* @return
* possible object is
* {@link String }
*
*/
public String getUserName() {
return userName;
}
/**
* 设置userName属性的值
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setUserName(String value) {
this.userName = value;
}
}

View File

@ -0,0 +1,62 @@
package com.xncoding.webservice.client;
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>getUserResponse complex type的 Java
*
* <p>以下模式片段指定包含在此类中的预期内容
*
* <pre>
* &lt;complexType name="getUserResponse">
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element name="return" type="{http://model.webservice.xncoding.com/}user" minOccurs="0"/>
* &lt;/sequence>
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getUserResponse", propOrder = {
"_return"
})
public class GetUserResponse {
@XmlElement(name = "return")
protected User _return;
/**
* 获取return属性的值
*
* @return
* possible object is
* {@link User }
*
*/
public User getReturn() {
return _return;
}
/**
* 设置return属性的值
*
* @param value
* allowed object is
* {@link User }
*
*/
public void setReturn(User value) {
this._return = value;
}
}

View File

@ -0,0 +1,115 @@
package com.xncoding.webservice.client;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the com.xncoding.webservice.client package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
private final static QName _GetUser_QNAME = new QName("http://model.webservice.xncoding.com/", "getUser");
private final static QName _GetUserResponse_QNAME = new QName("http://model.webservice.xncoding.com/", "getUserResponse");
private final static QName _SayHello_QNAME = new QName("http://model.webservice.xncoding.com/", "sayHello");
private final static QName _SayHelloResponse_QNAME = new QName("http://model.webservice.xncoding.com/", "sayHelloResponse");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.xncoding.webservice.client
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link GetUserResponse }
*
*/
public GetUserResponse createGetUserResponse() {
return new GetUserResponse();
}
/**
* Create an instance of {@link SayHello }
*
*/
public SayHello createSayHello() {
return new SayHello();
}
/**
* Create an instance of {@link GetUser }
*
*/
public GetUser createGetUser() {
return new GetUser();
}
/**
* Create an instance of {@link SayHelloResponse }
*
*/
public SayHelloResponse createSayHelloResponse() {
return new SayHelloResponse();
}
/**
* Create an instance of {@link User }
*
*/
public User createUser() {
return new User();
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link GetUser }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://model.webservice.xncoding.com/", name = "getUser")
public JAXBElement<GetUser> createGetUser(GetUser value) {
return new JAXBElement<GetUser>(_GetUser_QNAME, GetUser.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link GetUserResponse }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://model.webservice.xncoding.com/", name = "getUserResponse")
public JAXBElement<GetUserResponse> createGetUserResponse(GetUserResponse value) {
return new JAXBElement<GetUserResponse>(_GetUserResponse_QNAME, GetUserResponse.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link SayHello }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://model.webservice.xncoding.com/", name = "sayHello")
public JAXBElement<SayHello> createSayHello(SayHello value) {
return new JAXBElement<SayHello>(_SayHello_QNAME, SayHello.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link SayHelloResponse }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://model.webservice.xncoding.com/", name = "sayHelloResponse")
public JAXBElement<SayHelloResponse> createSayHelloResponse(SayHelloResponse value) {
return new JAXBElement<SayHelloResponse>(_SayHelloResponse_QNAME, SayHelloResponse.class, null, value);
}
}

View File

@ -0,0 +1,60 @@
package com.xncoding.webservice.client;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>sayHello complex type的 Java
*
* <p>以下模式片段指定包含在此类中的预期内容
*
* <pre>
* &lt;complexType name="sayHello">
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element name="userName" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* &lt;/sequence>
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayHello", propOrder = {
"userName"
})
public class SayHello {
protected String userName;
/**
* 获取userName属性的值
*
* @return
* possible object is
* {@link String }
*
*/
public String getUserName() {
return userName;
}
/**
* 设置userName属性的值
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setUserName(String value) {
this.userName = value;
}
}

View File

@ -0,0 +1,62 @@
package com.xncoding.webservice.client;
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>sayHelloResponse complex type的 Java
*
* <p>以下模式片段指定包含在此类中的预期内容
*
* <pre>
* &lt;complexType name="sayHelloResponse">
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element name="return" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* &lt;/sequence>
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayHelloResponse", propOrder = {
"_return"
})
public class SayHelloResponse {
@XmlElement(name = "return")
protected String _return;
/**
* 获取return属性的值
*
* @return
* possible object is
* {@link String }
*
*/
public String getReturn() {
return _return;
}
/**
* 设置return属性的值
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setReturn(String value) {
this._return = value;
}
}

View File

@ -0,0 +1,114 @@
package com.xncoding.webservice.client;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>user complex type的 Java
*
* <p>以下模式片段指定包含在此类中的预期内容
*
* <pre>
* &lt;complexType name="user">
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element name="age" type="{http://www.w3.org/2001/XMLSchema}int" minOccurs="0"/>
* &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}long" minOccurs="0"/>
* &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* &lt;/sequence>
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "user", propOrder = {
"age",
"id",
"name"
})
public class User {
protected Integer age;
protected Long id;
protected String name;
/**
* 获取age属性的值
*
* @return
* possible object is
* {@link Integer }
*
*/
public Integer getAge() {
return age;
}
/**
* 设置age属性的值
*
* @param value
* allowed object is
* {@link Integer }
*
*/
public void setAge(Integer value) {
this.age = value;
}
/**
* 获取id属性的值
*
* @return
* possible object is
* {@link Long }
*
*/
public Long getId() {
return id;
}
/**
* 设置id属性的值
*
* @param value
* allowed object is
* {@link Long }
*
*/
public void setId(Long 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;
}
}

View File

@ -0,0 +1,2 @@
@javax.xml.bind.annotation.XmlSchema(namespace = "http://model.webservice.xncoding.com/")
package com.xncoding.webservice.client;

View File

@ -1,5 +1,6 @@
package com.xncoding.webservice; package com.xncoding.webservice;
import com.xncoding.webservice.client.CommonService_Service;
import com.xncoding.webservice.model.User; import com.xncoding.webservice.model.User;
import com.xncoding.webservice.service.ICommonService; import com.xncoding.webservice.service.ICommonService;
import org.apache.cxf.endpoint.Client; import org.apache.cxf.endpoint.Client;
@ -12,6 +13,9 @@ import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApplicationTests { public class ApplicationTests {
@ -74,7 +78,7 @@ public class ApplicationTests {
} }
/** /**
* 方式2. 动态调用方式返回对象User * 方式3. 动态调用方式返回对象User
*/ */
@Test @Test
public void cl3() { public void cl3() {
@ -94,4 +98,15 @@ public class ApplicationTests {
} }
} }
/**
* 方式4. 客户端代码生成方式
*/
@Test
public void cl4() {
CommonService_Service c = new CommonService_Service();
com.xncoding.webservice.client.User user = c.getCommonServiceImplPort().getUser("Tom");
assertThat(user.getName(), is("Tom"));
}
} }