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

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

关于CXF要添加的Maven:

        
            org.apache.cxf
            cxf-rt-frontend-jaxws
            3.1.1
        

        
            org.apache.cxf
            cxf-rt-transports-http
            3.1.1
        

        
            org.apache.cxf
            cxf-rt-frontend-jaxrs
            3.1.1
        

        
            org.apache.cxf
            cxf-rt-transports-http-jetty
            3.1.1
        

        
            org.slf4j
            slf4j-simple
            1.7.12
        

使用CXF的流程:

1. 设置发布地址:

2. 设置访问接口;

3. 设置服务实现对象;

4. 通过工厂进行创建;

程序结构如下:

Java工作笔记-使用CXF接入及创建WebService_第1张图片

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

Java工作笔记-使用CXF接入及创建WebService_第2张图片

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

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

Java工作笔记-使用CXF接入及创建WebService_第3张图片

这里要配置好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

 

 

你可能感兴趣的:(webservice,Java,Spring,Boot,webservice,java,spring,spring,boot)