生成客户端方法
1.下载工具
官网地址:https://cxf.apache.org/download.html
腾讯镜像:https://mirrors.cloud.tencent.com/apache/cxf/
清华镜像:https://mirrors.tuna.tsinghua.edu.cn/apache/cxf/
更多镜像地址。。。
测试demo使用的是 apache-cxf-3.5.0.zip 基于java8运行
目前已知最新版是基于java11运行的
2.解压
解压压缩包,然后到\apache-cxf-3.5.0\bin目录 该目录下有 wsdl2java
3.执行命令
#Windows执行
wsdl2java -d D:\项目空间\WebServiceClient\lingdu -p com.lingdu.webservicenew.refund -client http://localhost:9000/ceshi?wsdl
#Linux执行
./wsdl2java -d /data/WebService/apache-cxf-3.5.0/bin/out -p com.lingdu.webservicenew.refund -client http://localhost:9000/ceshi?wsdl
#说明:
-d: 生成文件保存路径
-p: 生成代码包结构
-client: 远程wsdl地址
4.附说明
服务端部分代码
package example;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService()
public class ceshi {
@WebMethod
public String sayHelloWorldFrom(String from) {
String result = "Hello, world, from " + from;
System.out.println(result);
return result;
}
public static void main(String[] argv) {
Object implementor = new ceshi ();
String address = "http://localhost:9000/ceshi";
Endpoint.publish(address, implementor);
}
}
wsdl
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.7-b01 svn-revision#${svn.Last.Changed.Rev}. -->
<definitions targetNamespace="http://example" name="ceshi" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:tns="http://example" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata">
<import namespace="http://example/" location="CeshiPortType.wsdl"/>
<binding name="ceshiBinding" type="ns1:ceshi" xmlns:ns1="http://example/">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHelloWorldFrom">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="ceshi">
<port name="ceshi" binding="tns:ceshiBinding">
<soap:address location="REPLACE_WITH_ACTUAL_URL"/>
</port>
</service>
</definitions>
生成的代码
package com.lingdu.webservicenew.refund;
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.Action;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by Apache CXF 3.5.0
* 2022-02-22T15:34:37.858+08:00
* Generated source version: 3.5.0
*
*/
@WebService(targetNamespace = "http://example/", name = "ceshi")
@XmlSeeAlso({ObjectFactory.class})
public interface Ceshi {
@WebMethod
@Action(input = "http://example/ceshi/sayHelloWorldFromRequest", output = "http://example/ceshi/sayHelloWorldFromResponse")
@RequestWrapper(localName = "sayHelloWorldFrom", targetNamespace = "http://example/", className = "com.lingdu.webservicenew.refund.SayHelloWorldFrom")
@ResponseWrapper(localName = "sayHelloWorldFromResponse", targetNamespace = "http://example/", className = "com.lingdu.webservicenew.refund.SayHelloWorldFromResponse")
@WebResult(name = "return", targetNamespace = "")
public java.lang.String sayHelloWorldFrom(
@WebParam(name = "arg0", targetNamespace = "")
java.lang.String arg0
);
}
// 注意!!!!这里的两个className要和实际项目路径一致!!!!!
package com.lingdu.webservicenew.refund;
/**
* Please modify this class to meet your needs
* This class is not complete
*/
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
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.Action;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by Apache CXF 3.5.0
* 2022-02-22T15:34:37.806+08:00
* Generated source version: 3.5.0
*
*/
public final class Ceshi_CeshiPort_Client {
private static final QName SERVICE_NAME = new QName("http://example/", "ceshiService");
private Ceshi_CeshiPort_Client() {
}
public static void main(String args[]) throws java.lang.Exception {
URL wsdlURL = CeshiService.WSDL_LOCATION;
if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
File wsdlFile = new File(args[0]);
try {
if (wsdlFile.exists()) {
wsdlURL = wsdlFile.toURI().toURL();
} else {
wsdlURL = new URL(args[0]);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
CeshiService ss = new CeshiService(wsdlURL, SERVICE_NAME);
Ceshi port = ss.getCeshiPort();
{
System.out.println("Invoking sayHelloWorldFrom...");
java.lang.String _sayHelloWorldFrom_arg0 = "";
java.lang.String _sayHelloWorldFrom__return = port.sayHelloWorldFrom(_sayHelloWorldFrom_arg0);
System.out.println("sayHelloWorldFrom.result=" + _sayHelloWorldFrom__return);
}
System.exit(0);
}
}
package com.lingdu.webservicenew.refund;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;
import javax.xml.ws.Service;
/**
* This class was generated by Apache CXF 3.5.0
* 2022-02-22T15:34:37.866+08:00
* Generated source version: 3.5.0
*
*/
@WebServiceClient(name = "ceshiService",
wsdlLocation = "http://localhost:9000/ceshi?wsdl",
targetNamespace = "http://example/")
public class CeshiService extends Service {
public final static URL WSDL_LOCATION;
public final static QName SERVICE = new QName("http://example/", "ceshiService");
public final static QName CeshiPort = new QName("http://example/", "ceshiPort");
static {
URL url = null;
try {
url = new URL("http://localhost:9000/ceshi?wsdl");
} catch (MalformedURLException e) {
java.util.logging.Logger.getLogger(CeshiService.class.getName())
.log(java.util.logging.Level.INFO,
"Can not initialize the default wsdl from {0}", "http://localhost:9000/ceshi?wsdl");
}
WSDL_LOCATION = url;
}
public CeshiService(URL wsdlLocation) {
super(wsdlLocation, SERVICE);
}
public CeshiService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public CeshiService() {
super(WSDL_LOCATION, SERVICE);
}
public CeshiService(WebServiceFeature ... features) {
super(WSDL_LOCATION, SERVICE, features);
}
public CeshiService(URL wsdlLocation, WebServiceFeature ... features) {
super(wsdlLocation, SERVICE, features);
}
public CeshiService(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* @return
* returns Ceshi
*/
@WebEndpoint(name = "ceshiPort")
public Ceshi getCeshiPort() {
return super.getPort(CeshiPort, Ceshi.class);
}
/**
*
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* @return
* returns Ceshi
*/
@WebEndpoint(name = "ceshiPort")
public Ceshi getCeshiPort(WebServiceFeature... features) {
return super.getPort(CeshiPort, Ceshi.class, features);
}
}
package com.lingdu.webservicenew.refund;
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.lingdu.webservicenew.refund 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 _SayHelloWorldFrom_QNAME = new QName("http://example/", "sayHelloWorldFrom");
private final static QName _SayHelloWorldFromResponse_QNAME = new QName("http://example/", "sayHelloWorldFromResponse");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.lingdu.webservicenew.refund
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link SayHelloWorldFrom }
*
*/
public SayHelloWorldFrom createSayHelloWorldFrom() {
return new SayHelloWorldFrom();
}
/**
* Create an instance of {@link SayHelloWorldFromResponse }
*
*/
public SayHelloWorldFromResponse createSayHelloWorldFromResponse() {
return new SayHelloWorldFromResponse();
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link SayHelloWorldFrom }{@code >}
*
* @param value
* Java instance representing xml element's value.
* @return
* the new instance of {@link JAXBElement }{@code <}{@link SayHelloWorldFrom }{@code >}
*/
@XmlElementDecl(namespace = "http://example/", name = "sayHelloWorldFrom")
public JAXBElement<SayHelloWorldFrom> createSayHelloWorldFrom(SayHelloWorldFrom value) {
return new JAXBElement<SayHelloWorldFrom>(_SayHelloWorldFrom_QNAME, SayHelloWorldFrom.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link SayHelloWorldFromResponse }{@code >}
*
* @param value
* Java instance representing xml element's value.
* @return
* the new instance of {@link JAXBElement }{@code <}{@link SayHelloWorldFromResponse }{@code >}
*/
@XmlElementDecl(namespace = "http://example/", name = "sayHelloWorldFromResponse")
public JAXBElement<SayHelloWorldFromResponse> createSayHelloWorldFromResponse(SayHelloWorldFromResponse value) {
return new JAXBElement<SayHelloWorldFromResponse>(_SayHelloWorldFromResponse_QNAME, SayHelloWorldFromResponse.class, null, value);
}
}
@javax.xml.bind.annotation.XmlSchema(namespace = "http://example/")
package com.lingdu.webservicenew.refund;
package com.lingdu.webservicenew.refund;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>sayHelloWorldFrom complex type的 Java 类。
*
* <p>以下模式片段指定包含在此类中的预期内容。
*
* <pre>
* <complexType name="sayHelloWorldFrom">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="arg0" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayHelloWorldFrom", propOrder = {
"arg0"
})
public class SayHelloWorldFrom {
protected String arg0;
/**
* 获取arg0属性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getArg0() {
return arg0;
}
/**
* 设置arg0属性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setArg0(String value) {
this.arg0 = value;
}
}
package com.lingdu.webservicenew.refund;
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>sayHelloWorldFromResponse complex type的 Java 类。
*
* <p>以下模式片段指定包含在此类中的预期内容。
*
* <pre>
* <complexType name="sayHelloWorldFromResponse">
* <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>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayHelloWorldFromResponse", propOrder = {
"_return"
})
public class SayHelloWorldFromResponse {
@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;
}
}
测试
package example;
import com.Ceshi;
import com.CeshiService;
import com.pxgl.refund.DatacenterImp;
import com.pxgl.refund.DatacenterImpService;
/**
* @ClassName HelloWorld
* @Description TODO
* 作者:99013
* 创建时间:2022年02月22日 10:16:36
* @Version 1.0
**/
public class HelloWorld {
public static void main(String[] args) {
CeshiService ceshiService=new CeshiService();
Ceshi ceshiPort = ceshiService.getCeshiPort();
String lingdu = ceshiPort.sayHelloWorldFrom("零度");
System.out.println(lingdu);
}
}
//客户端输出结果
/*
D:\Java\jdk1.8.0\bin\java.exe "-javaagent:D:\软件\idea\2019\IntelliJ IDEA 2019.3.1\lib\idea_rt.jar=61312:D:\软件\idea\2019\IntelliJ IDEA 2019.3.1\bin" -Dfile.encoding=UTF-8 -classpath D:\Java\jdk1.8.0\jre\lib\charsets.jar;D:\Java\jdk1.8.0\jre\lib\deploy.jar;D:\Java\jdk1.8.0\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0\jre\lib\javaws.jar;D:\Java\jdk1.8.0\jre\lib\jce.jar;D:\Java\jdk1.8.0\jre\lib\jfr.jar;D:\Java\jdk1.8.0\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0\jre\lib\jsse.jar;D:\Java\jdk1.8.0\jre\lib\management-agent.jar;D:\Java\jdk1.8.0\jre\lib\plugin.jar;D:\Java\jdk1.8.0\jre\lib\resources.jar;D:\Java\jdk1.8.0\jre\lib\rt.jar;D:\项目空间\out\production\LingduWebService;D:\项目空间\LingduWebService\lib\mail.jar;D:\项目空间\LingduWebService\lib\ha-api.jar;D:\项目空间\LingduWebService\lib\policy.jar;D:\项目空间\LingduWebService\lib\stax-ex.jar;D:\项目空间\LingduWebService\lib\jaxb-api.jar;D:\项目空间\LingduWebService\lib\jaxb-xjc.jar;D:\项目空间\LingduWebService\lib\jaxws-rt.jar;D:\项目空间\LingduWebService\lib\mimepull.jar;D:\项目空间\LingduWebService\lib\saaj-api.jar;D:\项目空间\LingduWebService\lib\jaxb-impl.jar;D:\项目空间\LingduWebService\lib\jaxws-api.jar;D:\项目空间\LingduWebService\lib\saaj-impl.jar;D:\项目空间\LingduWebService\lib\stax2-api.jar;D:\项目空间\LingduWebService\lib\FastInoset.jar;D:\项目空间\LingduWebService\lib\jsr181-api.jar;D:\项目空间\LingduWebService\lib\jaxws-tools.jar;D:\项目空间\LingduWebService\lib\streambuffer.jar;D:\项目空间\LingduWebService\lib\gmbal-api-only.jar;D:\项目空间\LingduWebService\lib\management-api.jar;D:\项目空间\LingduWebService\lib\javax.annotation.jar;D:\项目空间\LingduWebService\lib\woodstox-core-asl.jar example.HelloWorld
Hello, world, from 零度
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
Process finished with exit code 0
*/
//服务端结果
/*
Hello, world, from 零度
*/