【笔记】SpringBoot+CXF整合发布WebService及客户端调用

WebService简单的实现

1.服务器端项目结构

2.pom.xml文件,

---引入Spring Boot web的jar包

---引入Spring-cxf jar包

<?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>com.cxfdemo</groupId>
    <artifactId>WebServiceCxf</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>
    <dependencies>
        <!--springboot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--cxf-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.3.2</version>
        </dependency>
    </dependencies>
</project>

3.服务接口

package com.cxfdemo.service;

import javax.jws.WebMethod;
import javax.jws.WebService;

/***
 * web services 接口
 */
@WebService(targetNamespace = "http://service.cxfdemo.com")// 命名空间,一般是接口的包名倒序
public interface WebServiceHelloWorld {
    @WebMethod//可省略,标注该方法为webservice暴露的方法,用于向外公布,它表示修饰的方法是webservice方法
    public String sayHello(String name);
}

4.服务接口实现类

package com.cxfdemo.service;
import org.springframework.stereotype.Component;
import javax.jws.WebService;

@WebService(serviceName = "helloWorld",//对外发布服务名
        targetNamespace = "http://service.cxfdemo.com",//包报名倒叙,并且和接口定义保持一致
        endpointInterface = "com.cxfdemo.service.WebServiceHelloWorld"///接口权限定名,指定做SEI(Service EndPoint Interface)服务端点接口
)
@Component
public class WebServiceHelloWorldImpl implements WebServiceHelloWorld {
    @Override
    public String sayHello(String name) {
        System.out.println("收到客户端发送的姓名:"+name);
        return "hello:"+name;
    }
}

5.发布WebService配置类,注多个发布需要多个服务接口和多个@Bean  Endpoint

package com.cxfdemo.config;

import com.cxfdemo.service.WebServiceHelloWorld;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;

/**
 * cxf发布webservice配置
 * 注:多个服务发布则需要多个服务接口,和多个Endpoint @Bean
 */
@Configuration
public class CxfConfig {
    @Autowired
    private Bus bus;
    @Autowired
    private WebServiceHelloWorld webServiceHelloWorld;


    @Bean
    public Endpoint endpoint(){
        EndpointImpl endpoint = new EndpointImpl(bus,webServiceHelloWorld);//绑定要发布的服务
        endpoint.publish("/hello");//发布的路径,此时访问路径为http://localhost:8080/services/hello?wsdl
        return endpoint;
    }

    /**
     * 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问
     * 默认服务发布在http://localhost:port/services/*路径下 ,如需改变默认路径可配置ServletRegistrationBean,即配置此方法
     * 例如
     * 去掉注释后:访问地址为:http://localhost:8080/soap/hello?wsdl
     */
//    @SuppressWarnings("all")
//    @Bean
//    public ServletRegistrationBean dispatcherServlet() {
//        return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
//    }
//----------------------------------------------------------------------------------------
    /**
     * 多个发布例子
     * 第二个服务发布
     */
//    @Autowired
//    private Interface2 interface2;
//    @Bean
//    public Endpoint endpoint2(){
//        EndpointImpl endpoint = new EndpointImpl(bus,Interface2);
//        endpoint.publish("/hello222");
//        return endpoint;
//    }
}

6.运行程序,输入 http://localhost:8080/service/hello?wsdl 即可查询发布出去的接口文件;

Application启动类代码,运行启动SpringBoot程序

package com.cxfdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

运行访问后wsdl文件截图

到此服务发布完成

7.测试---cxf客户端调用webservice接口

7.1客户端项目结构

7.2 添加cxf依赖

   <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.3.2</version>
    </dependency>

7.3客户端调用WebService服务类

package com.cxf.demo;


import com.cxf.demo.service.WebServiceHelloWorld;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * webservice客户端:
 * 该类提供两种不同的方式调用WebService服务
 * ----1.动态调用
 * ----2.代理工厂方式
 */
public class Client {

    public static void main(String[] args) {
//        Client.dynamicCall();//动态调用
        Client.proxyCall();//代理工厂
    }

    /**
     * 调用方式一:动态调用webservice
     */
    public static void dynamicCall(){
        //创建动态客户端
        JaxWsDynamicClientFactory jaxWsDynamicClientFactory = JaxWsDynamicClientFactory.newInstance();
        org.apache.cxf.endpoint.Client client = jaxWsDynamicClientFactory.createClient("http://localhost:8080/services/hello?wsdl");
        try {
            //invoke("方法名",参数1,参数2,参数3....);
            Object[] object=client.invoke("sayHello","小明");
            System.out.println("服务器返回结果"+object[0]);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 调用方式二:代理工厂方式,该方式需要拿到对方的接口地址
     */
    public static void proxyCall(){
        try{
            //接口地址
            String address="http://localhost:8080/services/hello?wsdl";
            //代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            //设置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            //设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(WebServiceHelloWorld.class);
            //创建一个代理接口实现
            WebServiceHelloWorld webServiceHelloWorld = (WebServiceHelloWorld)jaxWsProxyFactoryBean.create();
            //调用代理接口方法并获取返回结果
            String s = webServiceHelloWorld.sayHello("小红");
            System.out.println("服务器返回结果 "+s);//打印返回结果
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

7.4方式一,动态调用效果截图,

服务器:

客户端:

7.5代理工厂方式调用效果截图

此处服务器不需要重启,只要注释掉动态调用,去掉代理工厂的注释即可

服务器:

客户端

webservice服务发布测试完成

代码链接:https://pan.baidu.com/s/1WgmUf8w-THrkNOSkD2OX8g 提取码:ujwp 
 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值