远方的灯塔 - 专注于服务端技术分享 远方的灯塔 - 专注于服务端技术分享
首页
  • Java SE
  • Struts2
  • Hibernate
  • MyBatis
  • JAX-WS
  • 并发
  • 分布式
  • Git
  • 文章分类
  • 文章标签
  • 文章归档
  • 《C程序设计语言》
心情随笔
友情链接
给我留言 (opens new window)
关于我
GitHub (opens new window)

Terwer Green

一个后端老菜鸟
首页
  • Java SE
  • Struts2
  • Hibernate
  • MyBatis
  • JAX-WS
  • 并发
  • 分布式
  • Git
  • 文章分类
  • 文章标签
  • 文章归档
  • 《C程序设计语言》
心情随笔
友情链接
给我留言 (opens new window)
关于我
GitHub (opens new window)
  • JavaSE

  • 开源框架

  • Linux

  • Struts2

  • Hibernate

  • Webservice

    • Apache-CXF简介与第一个JAX-WS的入门程序
      • CXF 的历史
      • 稳定版本
      • 入门项目
        • 服务端
        • 客户端
        • 测试
      • 与.NET 交互,通过.NET 作为客户端链接 Webservice 服务
        • VS2022 版本
        • Rider 版本(可以用.NET Core 6)
  • 分布式

  • 分布式框架

  • 后端开发
  • Webservice
terwer
2022-11-05
目录

Apache-CXF简介与第一个JAX-WS的入门程序

# CXF 的历史

官网:https://cxf.apache.org/ (opens new window)

Celtix 和 XFire 合并而来。

# 稳定版本

3.3.11

https://archive.apache.org/dist/cxf/3.3.11/ (opens new window)

# 入门项目

新建一个普通 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>
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

服务端核心代码

MyService 接口

package space.terwer.cxf;

import javax.jws.WebService;

@WebService
public interface MyService {
	String hello(String username);
}
1
2
3
4
5
6
7
8

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;
	}

}
1
2
3
4
5
6
7
8
9
10
11

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();
	}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 客户端

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

客户端核心代码:

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);
	}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 测试

​

# 与.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();
            }
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
  • 测试

    ​

# 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);
            }
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
  • 测试

    ​

编辑 (opens new window)
#客户端#版本#WS#Webservice#JAX-WS#CXF#Apache
上次更新: 2023/02/22, 13:47:25
Hibernate的关键API详解以及上手第一个Hibernate项目
RPC架构设计及IO模型

← Hibernate的关键API详解以及上手第一个Hibernate项目 RPC架构设计及IO模型→

最近更新
01
解决css部分border被圆角切掉之后圆角的边框消失问题
03-18
02
使用TypeScript开发一个自定义的Node-js前端开发脚手架
03-08
03
Github-Actions使用release-please实现自动发版
03-06
更多文章>
Theme by Vdoing | Copyright © 2011-2023 Terwer Green | MIT License | 粤ICP备2022020721号-1 | 百度统计
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式