CXF 的历史 官网:https://cxf.apache.org/
Celtix 和 XFire 合并而来。
稳定版本 3.3.11
https://archive.apache.org/dist/cxf/3.3.11/
入门项目 新建一个普通 Java 项目即可。
最好使用 Maven
服务端 pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 <?xml version="1.0" encoding="UTF-8" ?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <groupId > space.terwer</groupId > <artifactId > cxf-server-demo</artifactId > <version > 1.0-SNAPSHOT</version > <packaging > war</packaging > <name > cxf-server-demo</name > <url > https://terwer.space</url > <properties > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > <maven.compiler.source > 8</maven.compiler.source > <maven.compiler.target > 8</maven.compiler.target > <cxf.version > 3.3.11</cxf.version > </properties > <dependencies > <dependency > <groupId > ch.qos.logback</groupId > <artifactId > logback-classic</artifactId > <version > 1.2.10</version > <scope > compile</scope > </dependency > <dependency > <groupId > ch.qos.logback</groupId > <artifactId > logback-core</artifactId > <version > 1.2.10</version > <scope > compile</scope > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > slf4j-api</artifactId > <version > 1.7.33</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > jul-to-slf4j</artifactId > <version > 1.7.33</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > jcl-over-slf4j</artifactId > <version > 1.7.33</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > log4j-over-slf4j</artifactId > <version > 1.7.33</version > </dependency > <dependency > <groupId > org.apache.logging.log4j</groupId > <artifactId > log4j-to-slf4j</artifactId > <version > 2.17.2</version > </dependency > <dependency > <groupId > org.apache.cxf</groupId > <artifactId > cxf-rt-frontend-jaxws</artifactId > <version > ${cxf.version}</version > </dependency > <dependency > <groupId > org.apache.cxf</groupId > <artifactId > cxf-rt-transports-http-jetty</artifactId > <version > ${cxf.version}</version > </dependency > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > 4.13.2</version > <scope > test</scope > </dependency > </dependencies > <build > <pluginManagement > <plugins > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-war-plugin</artifactId > <version > 3.3.2</version > </plugin > </plugins > </pluginManagement > </build > </project >
服务端核心代码
MyService 接口
1 2 3 4 5 6 7 8 package space.terwer.cxf;import javax.jws.WebService;@WebService public interface MyService { String hello (String username) ; }
MyServiceImpl 实现类
1 2 3 4 5 6 7 8 9 10 11 package space.terwer.cxf;public class MyServiceImpl implements MyService { @Override public String hello (String username) { System.out.println("hello is invoked!" ); return "hello," + username; } }
MyServer 服务端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package space.terwer.cxf;import org.apache.cxf.endpoint.Server;import org.apache.cxf.jaxws.JaxWsServerFactoryBean;public class MyServer { public static void main (String[] args) { JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean (); factory.setServiceClass(MyServiceImpl.class); factory.setAddress("http://localhost:8080/myservice?wsdl" ); Server server = factory.create(); server.start(); } }
客户端 pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 <?xml version="1.0" encoding="UTF-8" ?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <groupId > space.terwer</groupId > <artifactId > cxf-client-demo</artifactId > <version > 1.0-SNAPSHOT</version > <name > cxf-client-demo</name > <url > https://terwer.space</url > <properties > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > <maven.compiler.encoding > UTF-8</maven.compiler.encoding > <jdk.version > 1.8</jdk.version > <maven.compiler.source > 1.8</maven.compiler.source > <maven.compiler.target > 1.8</maven.compiler.target > <cxf.version > 3.3.11</cxf.version > </properties > <dependencies > <dependency > <groupId > ch.qos.logback</groupId > <artifactId > logback-classic</artifactId > <version > 1.2.10</version > <scope > compile</scope > </dependency > <dependency > <groupId > ch.qos.logback</groupId > <artifactId > logback-core</artifactId > <version > 1.2.10</version > <scope > compile</scope > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > slf4j-api</artifactId > <version > 1.7.33</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > jul-to-slf4j</artifactId > <version > 1.7.33</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > jcl-over-slf4j</artifactId > <version > 1.7.33</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > log4j-over-slf4j</artifactId > <version > 1.7.33</version > </dependency > <dependency > <groupId > org.apache.logging.log4j</groupId > <artifactId > log4j-to-slf4j</artifactId > <version > 2.17.2</version > </dependency > <dependency > <groupId > org.apache.cxf</groupId > <artifactId > cxf-rt-frontend-jaxws</artifactId > <version > ${cxf.version}</version > </dependency > <dependency > <groupId > org.apache.cxf</groupId > <artifactId > cxf-rt-transports-http-jetty</artifactId > <version > ${cxf.version}</version > </dependency > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > 4.13.1</version > <scope > test</scope > </dependency > </dependencies > <build > <plugins > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-surefire-plugin</artifactId > <configuration > <testFailureIgnore > true</testFailureIgnore > </configuration > </plugin > </plugins > </build > </project >
客户端核心代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package space.terwer.cxf;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class Client { public static void main (String[] args) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean (); factory.setAddress("http://localhost:8080/myservice?wsdl" ); factory.setServiceClass(MyService.class); MyService myservice = (MyService) factory.create(); String result = myservice.hello("terwer" ); System.out.println("result=>" + result); } }
测试
与.NET 交互,通过.NET 作为客户端链接 Webservice 服务 VS2022 版本
Rider 版本(可以用.NET Core 6)
新建一个 C#
的控制台项目
添加引用,输入地址
添加完成,点击会自动解析
调用客户端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 using WebServiceTest.MyServiceImplService; namespace WebServiceTest{ class Program { static void Main (string[] args) { Console.WriteLine("Hello World" ); MyServiceClient client = new MyServiceClient (); Task<helloResponse> respone = client.helloAsync("lisi" ); respone.Wait(); string result = respone.Result.Body.@return ; Console.WriteLine("result=>" + result); } } }
测试