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

Terwer CXF评论144字数 6892阅读22分58秒阅读模式

CXF 的历史

官网:https://cxf.apache.org/文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

Celtix 和 XFire 合并而来。文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

稳定版本

3.3.11文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

https://archive.apache.org/dist/cxf/3.3.11/文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

入门项目

新建一个普通 Java 项目即可。文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

最好使用 Maven文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

服务端

pom.xml文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

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

服务端核心代码文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

MyService 接口文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

package space.terwer.cxf;

import javax.jws.WebService;

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

MyServiceImpl 实现类文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

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 服务端文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

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文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

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

客户端核心代码:文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

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

}

测试

Apache-CXF简介与第一个JAX-WS的入门程序文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

与.NET 交互,通过.NET 作为客户端链接 Webservice 服务

VS2022 版本

  • 新建一个 .NET Framework​ 的控制台项目,注意不能选择 .NET Core​ ,目前我选择的是 .NET Framework 4.8.1

    Apache-CXF简介与第一个JAX-WS的入门程序文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

  • 新建 Web 引用,输入地址文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

    Apache-CXF简介与第一个JAX-WS的入门程序文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

  • 新建客户端文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

    ```c#
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

    namespace WebserviceNet4
    {
    internal class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("Hello World");文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

    <pre><code> hello.MyServiceImplService service = new hello.MyServiceImplService();
    string result = service.hello("terwer");
    Console.WriteLine(result);文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

    Console.ReadKey();
    }
    </code></pre>文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

    }
    }
    ```文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

  • 测试文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

    Apache-CXF简介与第一个JAX-WS的入门程序文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

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

  • 新建一个 C#​ 的控制台项目
  • 添加引用,输入地址文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

    Apache-CXF简介与第一个JAX-WS的入门程序文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

    添加完成,点击会自动解析文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

    Apache-CXF简介与第一个JAX-WS的入门程序文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

  • 调用客户端文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

    ```c#
    // See https://aka.ms/new-console-template for more information文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

    using WebServiceTest.MyServiceImplService;文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

    namespace WebServiceTest{
    class Program{
    static void Main(string[] args){
    Console.WriteLine("Hello World");文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

    <pre><code> MyServiceClient client = new MyServiceClient();
    Task<helloResponse> respone = client.helloAsync("lisi");
    respone.Wait();
    string result = respone.Result.Body.@return;
    Console.WriteLine("result=>" + result);
    }
    </code></pre>文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

    }
    }
    ```文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

  • 测试文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

    Apache-CXF简介与第一个JAX-WS的入门程序文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html

文章源自浅海拾贝-https://blog.terwergreen.com/introduction-to-apachecxf-and-the-entry-program-for-the-first-jaxws-286lne.html
相关文章
  • 扫码加我微信
  • 验证消息请输入:来自你的博客
  • weinxin
  • 我的微信公众号
  • 微信扫一扫与我交流吧
  • weinxin
Terwer
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: