首页 技术 正文
技术 2022年11月14日
0 收藏 585 点赞 3,986 浏览 4513 个字

SpringBoot2.1.6 整合CXF 实现Webservice

前言

最近LZ产品需要对接公司内部通讯工具,采用的是Webservice接口。产品框架用的SpringBoot2.1.6,于是采用整合CXF的方式实现Webservice接口。在这里分享下整合的demo。

代码实现
  1. 项目结构

    直接通过idea生成SpringBoot项目,也可以在http://start.spring.io生成。过于简单,这里不赘述。

    ![1561728847121](

)

  1. POM文件引入。这里引入的版本是3.2.4

    <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.4</version>
    </dependency>
  2. 接口与接口实现类

    package com.xiaoqiang.cxf.service;
    import com.xiaoqiang.cxf.entity.Student;
    import javax.jws.WebMethod;
    import javax.jws.WebService;
    import java.util.List;/**
    * IStudentService <br>
    * 〈〉
    *
    * @author XiaoQiang
    * @create 2019-6-27
    * @since 1.0.0
    */
    @WebService(targetNamespace = "http://service.cxf.xiaoqiang.com/") //命名一般是接口类的包名倒序
    public interface IStudentService { @WebMethod //声明暴露服务的方法,可以不写
    List<Student> getStudentInfo();
    }
    package com.xiaoqiang.cxf.service.impl;import com.xiaoqiang.cxf.entity.Student;
    import com.xiaoqiang.cxf.service.IStudentService;
    import org.springframework.stereotype.Component;
    import javax.jws.WebService;
    import java.util.ArrayList;
    import java.util.List;
    /**
    * StudentServiceImpl <br>
    * 〈学生接口实现类〉
    *
    * @author XiaoQiang
    * @create 2019-6-27
    * @since 1.0.0
    */
    @WebService(serviceName = "studentService"//服务名
    ,targetNamespace = "http://service.cxf.xiaoqiang.com/" //报名倒叙,并且和接口定义保持一致
    ,endpointInterface = "com.xiaoqiang.cxf.service.IStudentService")//包名
    @Component
    public class StudentServiceImpl implements IStudentService { @Override
    public List<Student> getStudentInfo() {
    List<Student> stuList = new ArrayList<>();
    Student student = new Student();
    student.setAge(18);
    student.setScore(700);
    student.setName("小强");
    stuList.add(student);
    return stuList; }
    }
  3. 配置类

    package com.xiaoqiang.cxf.config;import com.xiaoqiang.cxf.service.IStudentService;
    import org.apache.cxf.Bus;
    import org.apache.cxf.jaxws.EndpointImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import javax.xml.ws.Endpoint;
    /**
    * CxfConfig <br>
    * 〈cxf配置类〉
    * @desription cxf发布webservice配置
    * @author XiaoQiang
    * @create 2019-6-27
    * @since 1.0.0
    */
    @Configuration
    public class CxfConfig { @Autowired
    private Bus bus; @Autowired
    private IStudentService studentService; /**
    * 站点服务
    * @return
    */
    @Bean
    public Endpoint studentServiceEndpoint(){
    EndpointImpl endpoint = new EndpointImpl(bus,studentService);
    endpoint.publish("/studentService");
    return endpoint;
    }
    }
  4. 启动Application

    http://ip:端口/项目路径/services/studentService?wsdl 查看生成的wsdl

测试
package com.xiaoqiang.cxf;import com.xiaoqiang.cxf.entity.Student;
import com.xiaoqiang.cxf.service.IStudentService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;@RunWith(SpringRunner.class)
@SpringBootTest
public class CxfApplicationTests {
private Logger logger = LoggerFactory.getLogger(CxfApplication.class);
@Test
public void contextLoads() {
}
/**
* 方法一:动态客户端调用
*/
@Test
public void DynamicClient(){JaxWsDynamicClientFactory jwdcf = JaxWsDynamicClientFactory.newInstance();
Client client = jwdcf.createClient("http://localhost:8080/services/studentService?wsdl");
Object[] objects = new Object[0];try {
objects = client.invoke("getStudentInfo");
logger.info("获取学生信息==>{}",objects[0].toString());
System.out.println("invoke实体:"+((ArrayList) objects[0]).get(0).getClass().getPackage());
for(int i=0 ; i< ((ArrayList)objects[0]).size() ; i++){
Student student = new Student();
BeanUtils.copyProperties(((ArrayList) objects[0]).get(0),student);
logger.info("DynamicClient方式,获取学生{}信息==> 姓名:{},年龄:{},分数:{}",i+1,
student.getName(),student.getAge(),student.getScore());
}
} catch (Exception e) {
e.printStackTrace();
}
}/**
* 代理类工厂
*/
@Test
public void ProxyFactory(){String address = "http://localhost:8080/services/studentService?wsdl";
//代理工厂
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
//设置代理地址
factoryBean.setAddress(address);
//设置接口类型
factoryBean.setServiceClass(IStudentService.class);
//创建一个代理接口实现
IStudentService studentService = (IStudentService) factoryBean.create();
List<Student> studentList = studentService.getStudentInfo();
for(int i=0 ; i< studentList.size() ; i++){
Student student = studentList.get(i);
logger.info("ProxyFactory方式,获取学生{}信息==> 姓名:{},年龄:{},分数:{}",i+1,
student.getName(),student.getAge(),student.getScore());
}
}}
总结

1.接口与实现类中targetNamespace的注解是一定要写的,指明能够访问的接口

2.targetNamespace,最后面有一个斜线,通常是接口报名的反向顺序

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297