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
*/
@Override
public 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发布WebService
JaxWsServerFactoryBean 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);
//CXF
JaxWsProxyFactoryBean 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服务以及客户端调用

    这篇随笔内容是CXF发布webService服务以及客户端调用的方法 CXF是什么? 开发工作之前需要下载CXF和安装 下载地址:http://cxf.apache.org 安装过程: <1&g ...

  2. spring集成cxf实现webservice接口功能

    由于cxf的web项目已经集成了Spring,所以cxf的服务类都是在spring的配置文件中完成的.以下是步骤:第一步:建立一个web项目.第二步:准备所有jar包.将cxf_home\lib项目下 ...

  3. SSH集成cxf 发布restful webservice

    首先讲一下什么是restful webservice ,这个问题网上一搜有很多博文去长篇大论的介绍它,但是最后你看完了也会觉得云里雾里的,所以我在这里简单的讲一下我理解的rest webservice ...

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

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

  5. Spring Boot+CXF搭建WebService(转)

    概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...

  6. struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

    Cxf + Spring+ myeclipse+ cxf 进行  Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...

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

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

  8. SpringBoot整合cxf发布webService

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

  9. SpringMVC4整合CXF发布WebService

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

随机推荐

  1. CListCtrl 控件即使跟新数据,即时刷新以及属性设置

    用 m_CtrItem.Update( i );来即使跟新每行的数据,因为有时用某些函数如SetItemText()来设置某一行一列的数据是,控件上面的显示数据没有即使跟新,这是就有update来跟新 ...

  2. 计算机组成原理Day-1

  3. Python之浅谈生成器

    目录 三元表达式 列表推导式 字典生成式 生成器 生成器表达式 匿名函数 三元表达式 a=0 b=6 print (a)if a>b else print(b) 三元表达式只能写if的双分支结构 ...

  4. STL初步学习(map)

    3.map map作为一个映射,有两个参数,第一个参数作为关键值,第二个参数为对应的值,关键值是唯一的 在平时使用的数组中,也有点类似于映射的方法,例如a[10]=1,但其实我们的关键值和对应的值只能 ...

  5. Angular2-------Error: Unexpected value ‘undefined’ declared by the module ‘模块名

    请检查[app.module.ts]文件中的[declarations]模块最后是否多了一个逗号 (完)

  6. web网页多语言的实现方案_前端实现多语言切换

    实现的效果 需要在web中实现多语言的切换,当用户语言切换完成后下次重新打开网页,也是上次设置的语言进行显示. 资源网站搜索大全https://55wd.com 实现步骤 1.在用户点击切换语言后,把 ...

  7. LeetCode题解【题2】:两数相加

    原题链接:https://leetcode-cn.com/problems/add-two-numbers/ 查看请另起链接打开. 解题思路执行用时 :2 ms, 在所有 Java 提交中击败了99. ...

  8. CodeFoeces 1215 D Ticket Game(数学,思维)

    CodeFoeces 1215 D Ticket Game 题目大意 M和B轮流玩游戏(每一轮M先手 现在给出一个长度为偶数的串,包含字符'?'和数字 现在两人依次在'?'上填数字\(0\)~\(9\ ...

  9. Least Cost Bracket Sequence,题解

    题目链接 题意: 给你一个含有(,),?的序列,每个?变成(或)有一定的花费,问变成课匹配的括号的最小花费. 分析: 首先如果能变成匹配的,那么就有右括号的个数始终不多于左括号且左右括号数量相等,那就 ...

  10. opencv3.4.9 + armv7 + arm-linux-gnueabihf交叉编译

    使用CMake指定交叉编译链会有很多报错,原因可能是其找交叉编译的库或这头文件会自动链接到本地的库或者头文件. 可以使用Qt设置好交叉编译环境后,将CMakeLists.txt文件放入,直接编译通过即 ...