Java工作笔记-使用CXF接入及创建WebService

这里我采用Spring Boot进行项目启动。

关于CXF要添加的Maven:

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>3.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.12</version>
        </dependency>

使用CXF的流程:

1. 设置发布地址:

2. 设置访问接口;

3. 设置服务实现对象;

4. 通过工厂进行创建;

程序结构如下:

weatherService.java

package cn.demo.webService;

import javax.jws.WebService;

@WebService
public interface WeatherService {

    String query(String cityName);
}

WeatherServiceImpl.java

package cn.demo.webService;

import java.util.Random;

public class WeatherServiceImpl implements WeatherService {


    public String query(String cityName) {

        Random random = new Random();
        int n = random.nextInt(3);
        System.out.println("The n is " + n);
        String ret = "null";
        switch (n){

            case 0:
                ret = "晴天";
                break;
            case 1:
                ret = "阴天";
                break;
            case 3:
                ret = "雨天";
        }
        return ret;
    }
}

Main.java

package cn.demo;


import cn.demo.webService.WeatherService;
import cn.demo.webService.WeatherServiceImpl;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main implements CommandLineRunner {

    public static void main(String[] args){

        SpringApplication.run(Main.class, args);
    }

    public void run(String... args) throws Exception {

        JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();

        //设置发布的地址
        factoryBean.setAddress("http://localhost:9998/weatherService");

        //设置访问接口
        factoryBean.setServiceClass(WeatherService.class);

        //设置服务的实现对象
        factoryBean.setServiceBean(new WeatherServiceImpl());

        //通过工厂创建服务
        factoryBean.create();
        System.out.println("发布服务成功!");
    }
}

项目源码如下:

https://github.com/fengfanchen/Java/tree/master/CXFService

 

下面是接入:

同样也是采用Spring Boot启动

在此目录下运行wsdl2java

E:\IdeaProject2019\CallOtherWebService\CXFClient\src\main\java>wsdl2java -d . http://localhost:9998/weatherService?wsdl

生成的东西都是在webservice文件夹中。

这里wsdl2java在互联网上下载就可以了!

这里要配置好java环境变量以及添加CXF环境变量。

访问逻辑如下:

1. 设置服务地址;

2. 设置服务接口;

3. 通过工厂获取对象;

4. 调用接口;

源码如下Main.java

package cn.demo;

import cn.demo.webservice.WeatherService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main implements CommandLineRunner {

    public static void main(String[] args){

        SpringApplication.run(Main.class, args);
    }

    public void run(String... strings) throws Exception {

        JaxWsProxyFactoryBean proxyFactoryBean = new JaxWsProxyFactoryBean();

        //设置服务地址
        proxyFactoryBean.setAddress("http://localhost:9998/weatherService");

        //设置服务接口
        proxyFactoryBean.setServiceClass(WeatherService.class);

        //通过工厂获取对象
        WeatherService service = (WeatherService)proxyFactoryBean.create();
        String nj = service.query("南京");

        System.out.println(nj);
    }
}

源码下载地址:

https://github.com/fengfanchen/Java/tree/master/CXFClient

 

 

©️2020 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值