Spring集成CXF发布WebService

1.导入jar包

因为官方下载的包里面有其他版本的sprring包,全导入会产生版本冲突,所以去掉spring的部分,然后在项目根目录下新建了一个CXF_lib目录,保存jar包。

2.编写PianoInterface接口

新建一个PianoInterface接口定义方法,并添加注解@WebService

package com.CXF;import javax.jws.WebService;@WebService
public interface PianoInterface {//根据Brand查询价格public int getPriceByBrand(String brand);
}

3.创建PianoService实现PianoInterface接口

package com.CXF;import com.service.PianoServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;import javax.jws.WebService;@WebService(endpointInterface = "com.CXF.PianoInterface")
public class PianoService implements PianoInterface {/*** @description 根据品牌查询价格* @param brand 品牌* @return int price 价格* @date 2020/4/2* @author Charlotte*/@Overridepublic int getPriceByBrand(String brand) {ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");PianoServiceImpl pianoService = (PianoServiceImpl) ctx.getBean("pianoServiceImpl");return pianoService.getPriceByBrand(brand);}
}

4.修改spring配置文件

修改applicationContext.xml文件
添加

xmlns:jaxws="http://cxf.apache.org/jaxws"
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd

添加

<!-- 配置发布webservice服务 --><jaxws:server address="/PianoWs"serviceClass="com.CXF.PianoService"><jaxws:serviceBean><bean class="com.CXF.PianoService"></bean></jaxws:serviceBean></jaxws:server>

配置web.xml

<!--    CXF配置Servlet--><servlet><servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><!-- 初始化CXFServlet -->
<!--        <init-param>-->
<!--            <param-name>config-location</param-name>-->
<!--            <param-value>classpath:applicationContext.xml</param-value>-->
<!--        </init-param>--><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>cxf</servlet-name><url-pattern>/ws/*</url-pattern></servlet-mapping>

发布WebService

package com.ui;import com.CXF.PianoInterface;
import com.CXF.PianoService;
import com.webService.PianoWebService;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;import javax.xml.ws.Endpoint;public class test {public static void main(String[] args) {//JAVA自带的WebService发布类
//        PianoWebService pianoWebService = new PianoWebService();
//        Endpoint.   publish("http://localhost:8080/pianowebservice",pianoWebService);
//        System.out.println("启动webservice");//使用CXF发布WebServiceJaxWsServerFactoryBean jsfb = new JaxWsServerFactoryBean();//1.服务提供者实现的接口jsfb.setServiceClass(PianoInterface.class);//2.指定访问路径jsfb.setAddress("http://localhost:8080/ws");//3.指定服务实现类jsfb.setServiceBean(new PianoService());//jsfb.getInInterceptors().add(new LoggingInInterceptor());//jsfb.getOutInterceptors().add(new LoggingOutInterceptor());//4.发布jsfb.create();System.out.println("发布成功...");}}

发布完成之后可以访问http://localhost:8080/ws?wsdl

客户端调用WebService

1.获取java文件

cmd进入jdk下的bin目录,然后输入以下代码

C:\Program Files\Java\jdk1.8.0_73\bin>wsimport -d F:\ -s F:\ -p com http://localhost:8080/pianowebservice?wsdl

在上面指定的文件里得到生成的文件,然后复制java文件到项目中

2.客户端导入CXF的所有jar包,如果冲突可以不导入spring相关的jar包

编写test类

import client.PianoInterface;import client.PianoInterfaceService;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class test {public static void main(String[] args) {//JAVA原生
//        PianoInterfaceService pianoWebService = new PianoInterfaceService();
//        PianoInterface pianoService = pianoWebService.getPianoInterfacePort();
//        int price = pianoService.getPriceByBrand("IQOO");
//        System.out.println("获得价格:"+price);//CXFJaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();soapFactoryBean.setAddress("http://localhost:8080/ws");soapFactoryBean.setServiceClass(PianoInterface.class);Object o = soapFactoryBean.create();PianoInterface service = (PianoInterface) o;int price = service.getPriceByBrand("IQOO");System.out.println("获得价格:"+price);}
}

如果报

两个类具有相同的 XML 类型名称 "{http://webService.com/}getPriceResponse"。请使用 @XmlType.name 和 @XmlType.namespace 为类分配不同的名称

这个错可以在两个类里添加一个注解,namespace = "http://namespace.thats.not.the.same.as.the.generated"

3.运行

Spring集成CXF发布WebService并在客户端调用相关推荐

  1. 使用cxf发布webservice接口,以及调用webservice接口

    一.cxf发布webservice接口 添加maven依赖 <dependency><groupId>org.apache.cxf</groupId><art ...

  2. 使用CXF与Spring集成实现RESTFul WebService

    以下引用与网络中!!! 一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制. ...

  3. CXF发布webservice

    2019独角兽企业重金招聘Python工程师标准>>> CXF 是两个框架集合,基于XFire. 下载地址http://cxf.apache.org/download.html 我使 ...

  4. 使用CXF发布WebService服务简单实例

    一.说明: 前面介绍了使用axis2来发布Webservice服务,现在介绍一种更popular,更高效的Webservice服务发布技术:CXF Apache CXF = Celtix + XFir ...

  5. 关于用SoapUI集成CXF生成WebService客户端,以及测试的流程和问题的解决

    项目环境 1.springBoot框架的web系统 2.jdk1.8 3.windows 10 集成 IDEA开发环境 4.SoapUI 5.4.0软件(直接去百度搜索下载) 5.apache-cxf ...

  6. wsld2java_脱离spring集成cxf(基于nutz框架)

    什么是webService cxf 简单的说就是实现webService的一个比较流行的框架 http://blog.sina.com.cn/s/blog_6182547f01017pak.html ...

  7. Spring boot+CXF开发WebService Demo

    本文转载自: https://www.cnblogs.com/fuxin41/p/6289162.html 作者:fuxin41 转载请注明该声明. 最近工作中需要用到webservice,而且结合s ...

  8. cxf发布 webservice服务

    导包 antlr-2.7.7.jar aopalliance-1.0.jar asm-3.3.jar commons-collections-3.2.1.jar commons-lang-2.6.ja ...

  9. 服务端使用Axis2-1.6.3发布webservice服务、客户端使用Axis1.4实现调用

    一.准备工作 下载Axis2-1.6.3-war.zip 下载链接 下载Axis1.4相关jar包 下载链接 二.开发Webservice服务端代码 使用环境:myeclipse6.6+tomcat6 ...

最新文章

  1. 《DSP using MATLAB》示例 Example 6.25
  2. 悬而未决的AI竞赛:全球企业人工智能发展现状
  3. 嵌入式工程师必读100本专业书籍
  4. STL中的priority_queue(优先队列)
  5. Smalidea无源码调试 android 应用
  6. DI 之 3.4 Bean的作用域(捌)
  7. 100个程序员学习的网站
  8. python sizeof_python 变量作用域 v.__sizeof__() python 深复制 一切皆对象 尽量减少内存消耗 赋值语句的原理...
  9. 小白学测试(基础知识)
  10. oracle配置ipv6_配置 IPv6 接口
  11. arcgis oracle srid,ArcGIS——数据库空间SQL(二、oracle中ST_GEOMETRY函数使用)
  12. 记忆训练: 记数字 (110数字图像编码)
  13. 旋转式直流无刷Maxon电机与copley驱动器调试
  14. 栋的月结 | 第三回合(定期更新、动态、架构、云技术、算法、后端、前端、收听/收看、英文、书籍、影视、好歌、新奇)[含泪总结.. 憋泪分享!]
  15. 基于JAVA的校园二手交易平台(附:源码 论文 数据库文件)
  16. 宿主机和docker容器之间的文件拷贝
  17. 元认知能力-认知的理解
  18. 【bzoj 2844】: albus就是要第一个出场
  19. 2020新版idea创建web项目
  20. linux常用命令及ip地址更改

热门文章

  1. 计算机工程学院运动会方阵口号,运动会方阵口号(精选多篇)
  2. 简单的移动端图片裁剪vue插件[旋转,平移,缩放,印花]
  3. web安全的学习路线
  4. Leetcode学习成长记:天池leetcode基础训练营Task02链表
  5. Express初级学习
  6. Ubuntu 16.04服务器 配置
  7. 2021.9.11周六PAT甲级考试复盘与总结
  8. centos 7 下使用dvorak键盘布局
  9. 求生之路2联机服务器没有响应,求生之路2联机卡,为什么求生之路2联机进不去...
  10. Windows下配置Squid反向代理服务器