Apache-CXF簡介與第一個JAX-WS的入門程序

CXF 的歷史

官網:https://cxf.apache.org/

Celtix 和 XFire 合併而來。

穩定版本

3.3.11

https://archive.apache.org/dist/cxf/3.3.11/

入門項目

新建一個普通 Java 項目即可。

最好使用 Maven

服務端

pom.xml

<?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>
        <!-- Logback -->
        <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>

        <!-- cfx -->
        <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><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.3.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

服務端核心代碼

MyService 接口

package space.terwer.cxf;

import javax.jws.WebService;

@WebService
public interface MyService {
	String hello(String username);
}

MyServiceImpl 實現類

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 服務端

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

<?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>
        <!-- Logback -->
        <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>

        <!-- cfx -->
        <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>

客戶端核心代碼:

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 版本

  • 新建一個 .NET Framework​ 的控制檯項目,注意不能選擇 .NET Core​ ,目前我選擇的是 .NET Framework 4.8.1

  • 新建 Web 引用,輸入地址

  • 新建客戶端

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace WebserviceNet4
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World");
    
                hello.MyServiceImplService service = new hello.MyServiceImplService();
                string result = service.hello("terwer");
                Console.WriteLine(result);
    
                Console.ReadKey();
            }
        }
    }
    
  • 測試

Rider 版本(可以用.NET Core 6)

  • 新建一個 C#​ 的控制檯項目

  • 添加引用,輸入地址

    添加完成,點擊會自動解析

  • 調用客戶端

    // See https://aka.ms/new-console-template for more information
    
    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);
            }
        }
    }
    
  • 測試

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章