diff --git a/springboot-cxf/README.md b/springboot-cxf/README.md index eab02d4..900d01e 100644 --- a/springboot-cxf/README.md +++ b/springboot-cxf/README.md @@ -6,20 +6,44 @@ ## 客户端动态代理调用 -这个在单元测试类ApplicationTests中有演示 +这个在单元测试类ApplicationTests中有演示,这里要注意的是model类的包名一定要放到指定的路径下。 +也就是targetNamespace的倒叙包名中。 -## 生产客户端代码 +## 客户端代码生成 -apache的wsdl2java工具,使用`-autoNameResolution`自动处理 +有两种方式生成客户端调用代码 + +**Apache的wsdl2java工具** ``` 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")); ``` ## 许可证 diff --git a/springboot-cxf/src/main/java/com/xncoding/webservice/client/CommonService.java b/springboot-cxf/src/main/java/com/xncoding/webservice/client/CommonService.java new file mode 100644 index 0000000..6240889 --- /dev/null +++ b/springboot-cxf/src/main/java/com/xncoding/webservice/client/CommonService.java @@ -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); + +} diff --git a/springboot-cxf/src/main/java/com/xncoding/webservice/client/CommonService_Service.java b/springboot-cxf/src/main/java/com/xncoding/webservice/client/CommonService_Service.java new file mode 100644 index 0000000..3175ca9 --- /dev/null +++ b/springboot-cxf/src/main/java/com/xncoding/webservice/client/CommonService_Service.java @@ -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 features 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; + } + +} diff --git a/springboot-cxf/src/main/java/com/xncoding/webservice/client/GetUser.java b/springboot-cxf/src/main/java/com/xncoding/webservice/client/GetUser.java new file mode 100644 index 0000000..8d49ba5 --- /dev/null +++ b/springboot-cxf/src/main/java/com/xncoding/webservice/client/GetUser.java @@ -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; + + +/** + *

getUser complex type的 Java 类。 + * + *

以下模式片段指定包含在此类中的预期内容。 + * + *

+ * <complexType name="getUser">
+ *   <complexContent>
+ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *       <sequence>
+ *         <element name="userName" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
+ *       </sequence>
+ *     </restriction>
+ *   </complexContent>
+ * </complexType>
+ * 
+ * + * + */ +@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; + } + +} diff --git a/springboot-cxf/src/main/java/com/xncoding/webservice/client/GetUserResponse.java b/springboot-cxf/src/main/java/com/xncoding/webservice/client/GetUserResponse.java new file mode 100644 index 0000000..08fe0e5 --- /dev/null +++ b/springboot-cxf/src/main/java/com/xncoding/webservice/client/GetUserResponse.java @@ -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; + + +/** + *

getUserResponse complex type的 Java 类。 + * + *

以下模式片段指定包含在此类中的预期内容。 + * + *

+ * <complexType name="getUserResponse">
+ *   <complexContent>
+ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *       <sequence>
+ *         <element name="return" type="{http://model.webservice.xncoding.com/}user" minOccurs="0"/>
+ *       </sequence>
+ *     </restriction>
+ *   </complexContent>
+ * </complexType>
+ * 
+ * + * + */ +@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; + } + +} diff --git a/springboot-cxf/src/main/java/com/xncoding/webservice/client/ObjectFactory.java b/springboot-cxf/src/main/java/com/xncoding/webservice/client/ObjectFactory.java new file mode 100644 index 0000000..50f88f1 --- /dev/null +++ b/springboot-cxf/src/main/java/com/xncoding/webservice/client/ObjectFactory.java @@ -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. + *

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 createGetUser(GetUser value) { + return new JAXBElement(_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 createGetUserResponse(GetUserResponse value) { + return new JAXBElement(_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 createSayHello(SayHello value) { + return new JAXBElement(_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 createSayHelloResponse(SayHelloResponse value) { + return new JAXBElement(_SayHelloResponse_QNAME, SayHelloResponse.class, null, value); + } + +} diff --git a/springboot-cxf/src/main/java/com/xncoding/webservice/client/SayHello.java b/springboot-cxf/src/main/java/com/xncoding/webservice/client/SayHello.java new file mode 100644 index 0000000..cd53b8f --- /dev/null +++ b/springboot-cxf/src/main/java/com/xncoding/webservice/client/SayHello.java @@ -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; + + +/** + *

sayHello complex type的 Java 类。 + * + *

以下模式片段指定包含在此类中的预期内容。 + * + *

+ * <complexType name="sayHello">
+ *   <complexContent>
+ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *       <sequence>
+ *         <element name="userName" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
+ *       </sequence>
+ *     </restriction>
+ *   </complexContent>
+ * </complexType>
+ * 
+ * + * + */ +@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; + } + +} diff --git a/springboot-cxf/src/main/java/com/xncoding/webservice/client/SayHelloResponse.java b/springboot-cxf/src/main/java/com/xncoding/webservice/client/SayHelloResponse.java new file mode 100644 index 0000000..9464f46 --- /dev/null +++ b/springboot-cxf/src/main/java/com/xncoding/webservice/client/SayHelloResponse.java @@ -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; + + +/** + *

sayHelloResponse complex type的 Java 类。 + * + *

以下模式片段指定包含在此类中的预期内容。 + * + *

+ * <complexType name="sayHelloResponse">
+ *   <complexContent>
+ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *       <sequence>
+ *         <element name="return" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
+ *       </sequence>
+ *     </restriction>
+ *   </complexContent>
+ * </complexType>
+ * 
+ * + * + */ +@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; + } + +} diff --git a/springboot-cxf/src/main/java/com/xncoding/webservice/client/User.java b/springboot-cxf/src/main/java/com/xncoding/webservice/client/User.java new file mode 100644 index 0000000..24d7053 --- /dev/null +++ b/springboot-cxf/src/main/java/com/xncoding/webservice/client/User.java @@ -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; + + +/** + *

user complex type的 Java 类。 + * + *

以下模式片段指定包含在此类中的预期内容。 + * + *

+ * <complexType name="user">
+ *   <complexContent>
+ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *       <sequence>
+ *         <element name="age" type="{http://www.w3.org/2001/XMLSchema}int" minOccurs="0"/>
+ *         <element name="id" type="{http://www.w3.org/2001/XMLSchema}long" minOccurs="0"/>
+ *         <element name="name" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
+ *       </sequence>
+ *     </restriction>
+ *   </complexContent>
+ * </complexType>
+ * 
+ * + * + */ +@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; + } + +} diff --git a/springboot-cxf/src/main/java/com/xncoding/webservice/client/package-info.java b/springboot-cxf/src/main/java/com/xncoding/webservice/client/package-info.java new file mode 100644 index 0000000..c0fad79 --- /dev/null +++ b/springboot-cxf/src/main/java/com/xncoding/webservice/client/package-info.java @@ -0,0 +1,2 @@ +@javax.xml.bind.annotation.XmlSchema(namespace = "http://model.webservice.xncoding.com/") +package com.xncoding.webservice.client; diff --git a/springboot-cxf/src/test/java/com/xncoding/webservice/ApplicationTests.java b/springboot-cxf/src/test/java/com/xncoding/webservice/ApplicationTests.java index fd5124b..5a23140 100644 --- a/springboot-cxf/src/test/java/com/xncoding/webservice/ApplicationTests.java +++ b/springboot-cxf/src/test/java/com/xncoding/webservice/ApplicationTests.java @@ -1,5 +1,6 @@ package com.xncoding.webservice; +import com.xncoding.webservice.client.CommonService_Service; import com.xncoding.webservice.model.User; import com.xncoding.webservice.service.ICommonService; 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.test.context.junit4.SpringRunner; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class ApplicationTests { @@ -74,7 +78,7 @@ public class ApplicationTests { } /** - * 方式2. 动态调用方式,返回对象User + * 方式3. 动态调用方式,返回对象User */ @Test 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")); + } + }