首页 > 编程语言 >Apache-CXF简介与第一个JAX-WS的入门程序

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

时间:2022-11-08 19:24:21浏览次数:81  
标签:CXF JAX terwer space slf4j WS cxf apache org

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);
            }
        }
    }
    
  • 测试

标签:CXF,JAX,terwer,space,slf4j,WS,cxf,apache,org
From: https://www.cnblogs.com/tangyouwei/p/introduction-to-apachecxf-and-the-entry-program-for-t

相关文章

  • 释放windows预留内存,增加在用CPU个数提高整机性能
    释放windows预留内存,增加在用CPU个数提高整机性能windows系统更新后或重装后,会预留一部分内存(大概2G)。如果本机内存比较小,这就很伤。而且,默认系统使用1个CPU在跑,这样计算的......
  • 自定义垃圾筐的名称-Windows
    自定义垃圾筐的名称-Windows修改注册表就行了。这样做打开注册表​​\HKEY_CLASSES_ROOT\CLSID\{645FF040-5081-101B-9F08-00AA002F954E}​​​。​​右击权限-->高级-->更......
  • 在 windows 上安装 Redis
    在windows上安装RedisRedis官方不建议在windows下使用Redis,所以官网没有windows版本可以下载。还好微软团队维护了开源的windows版本,虽然只有3.2版本,对于......
  • 关于如何在Windows下通过Golang调用cmd指令
    太nm操蛋了,我tm弄了两小时。起因目前我的项目中,当并发量提高的时候会出现UDP的bufferqueuefull的情况,我怀疑是因为UDP端口释放太慢导致堆积。于是就打算用golang写一......
  • QProcess 调用.py脚本(windows + python 环境)
    1QProcessp;2QStringListargs1;3args1.append("demo.py");//设置py脚本4p.setWorkingDirectory("d:/demo/");//设置py脚本所在目录5p......
  • windows系统无法创建任务解决方案
        甲方安排一个任务,需要定时对局域网目录共享文件进行扫描,因为扫描共享文件这块已经通过SMB协议完成,我只需要把这个程序定时执行就可以完工。一开始有两个方案,......
  • Windows - win10 开放外部访问端口
    win10开放外部访问端口直接在搜索中输入“防火墙”,然后打开“防火墙高级设置”新建入站规则 选择端口,然后下一步选择连接协议,然后输入特定的端口号,然后下一步选......
  • 在Windows8_10中使用匿名共享
    1、打开本地组策略编辑器(快捷键Win+R,打开运行,输入gpedit.msc,确定);2、打开:“本地计算机策略->计算机配置->Windows设置->安全设置->本地策略->用户权限分配”;找到:“拒绝......
  • Windows平台安装OpenSSL
    Windows平台安装openssl有两种方法,第一种是直接下载安装包进行安装、第二种是下载源站进行编译安装1.直接下载安装包进行安装(最简单)1.1下载安装包Window的openssl的安装......
  • Windows下将压缩包隐写到图片中以及相对应的提取
    Windows下将压缩包隐写到图片中以及相对应的提取其实原理非常的简单,就是进行一个压缩,只不过让它看起来像图片一样;下面是效果:实现的原理是非常简单的,使用了Windows下的命......