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,最后面有一个斜线,通常是接口报名的反向顺序

SpringBoot2.1.6 整合CXF 实现Webservice的更多相关文章

  1. SpringMVC4整合CXF发布WebService

    SpringMVC4整合CXF发布WebService版本:SpringMVC 4.1.6,CXF 3.1.0项目管理:apache-maven-3.3.3 pom.xml <project x ...

  2. SpringBoot整合cxf发布webService

    1. 看看项目结构图 2. cxf的pom依赖 1 <dependency>2 <groupId>org.apache.cxf</groupId>3 <art ...

  3. spring mvc + mybaties + mysql 完美整合cxf 实现webservice接口 (服务端、客户端)

    spring-3.1.2.cxf-3.1.3.mybaties.mysql 整合实现webservice需要的完整jar文件 地址:http://download.csdn.net/detail/xu ...

  4. spring-boot整合Cxf的webservice案例

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.Maven Plugin管理 <?xml version="1.0&q ...

  5. Spring整合CXF之发布WebService服务

    今天我们来讲下如何用Spring来整合CXF,来发布WebService服务: 给下官方文档地址:http://cxf.apache.org/docs/writing-a-service-with-s ...

  6. Spring整合CXF,发布RSETful 风格WebService(转)

    Spring整合CXF,发布RSETful 风格WebService 这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有很大一部分都是一样的.关于发布CXF WebServer和Sp ...

  7. CXF整合Spring开发WebService

    刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开 ...

  8. Spring整合CXF步骤,Spring实现webService,spring整合WebService

    Spring整合CXF步骤 Spring实现webService, spring整合WebService >>>>>>>>>>>> ...

  9. Spring整合CXF,发布RSETful 风格WebService

    原文地址:http://www.cnblogs.com/hoojo/archive/2012/07/23/2605219.html 这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有 ...

随机推荐

  1. 从Client应用场景介绍IdentityServer4(四)

    原文:从Client应用场景介绍IdentityServer4(四) 上节以对话形式,大概说了几种客户端授权模式的原理,这节重点介绍Hybrid模式在MVC下的使用.且为实现IdentityServe ...

  2. Android UI法宝的发展Android Action Bar Style Generator

    ActionBar它是3.0经UI设计规格.同时它是Google设计风格强烈推荐,如何做一个高速设计的眼睛ActionBar之.进一步,我们设置了阶段为一个入眼ActionBar模板吧,然后,Andr ...

  3. Java--ConcurrentHashMap原理分析

    一.背景: 线程不安全的HashMap     因为多线程环境下,使用Hashmap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap.   效率低下的H ...

  4. Linux下编译,要下载tar.xz,而不要下载zip,因为换行的编码不一样,对.h.cpp没有影响,但是对脚本有影响 good

    原因是 在win下编辑的时候,换行结尾是\n\r , 而在linux下 是\n,所以才会有 多出来的\r但是这个我是直接下载的官网文件解压的,没有动过啊. 破案了. linux下编译要下 .tar.x ...

  5. 对XAML进行编辑的辅助类(XamlHelper)

    原文:对XAML进行编辑的辅助类(XamlHelper) // XamlHelper.cs// --------------------------------------------// 对XAML ...

  6. WPF 获取鼠标屏幕位置、窗口位置、控件位置

    原文:WPF 获取鼠标屏幕位置.窗口位置.控件位置 public struct POINT { public int X; public int Y; public POINT(int x, int ...

  7. .net reactor 学习系列(二)---.net reactor界面各功能说明

    原文:.net reactor 学习系列(二)---.net reactor界面各功能说明         安装了.net reactor之后,可以在安装目录下找到帮助文档REACTOR_HELP.c ...

  8. TestDisk 数据恢复 重建分区表恢复文件-恢复diskpart clean

    source:http://www.cgsecurity.org/wiki/TestDisk_CN TestDisk 是一款开源软件,受GNU General Public License (GPL ...

  9. FileHelper

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Win ...

  10. Winform入门见解

    winform算是C#比较快速的入门的一个了,简单的控件拖拽然后写上每个控件对应的事件.然后就可以了.需要美观的点 可以用Skin皮肤就完成了.我们先不说复杂的,就来个普通的三层架构来增删改查 分页和 ...