Spring集成CXF发布WebService并在客户端调用
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并在客户端调用的更多相关文章
- CXF发布webService服务以及客户端调用
这篇随笔内容是CXF发布webService服务以及客户端调用的方法 CXF是什么? 开发工作之前需要下载CXF和安装 下载地址:http://cxf.apache.org 安装过程: <1&g ...
- spring集成cxf实现webservice接口功能
由于cxf的web项目已经集成了Spring,所以cxf的服务类都是在spring的配置文件中完成的.以下是步骤:第一步:建立一个web项目.第二步:准备所有jar包.将cxf_home\lib项目下 ...
- SSH集成cxf 发布restful webservice
首先讲一下什么是restful webservice ,这个问题网上一搜有很多博文去长篇大论的介绍它,但是最后你看完了也会觉得云里雾里的,所以我在这里简单的讲一下我理解的rest webservice ...
- 使用CXF与Spring集成实现RESTFul WebService
以下引用与网络中!!! 一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存 ...
- Spring Boot+CXF搭建WebService(转)
概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...
- struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
Cxf + Spring+ myeclipse+ cxf 进行 Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...
- 使用CXF发布WebService服务简单实例
一.说明: 前面介绍了使用axis2来发布Webservice服务,现在介绍一种更popular,更高效的Webservice服务发布技术:CXF Apache CXF = Celtix + XFir ...
- SpringBoot整合cxf发布webService
1. 看看项目结构图 2. cxf的pom依赖 1 <dependency>2 <groupId>org.apache.cxf</groupId>3 <art ...
- SpringMVC4整合CXF发布WebService
SpringMVC4整合CXF发布WebService版本:SpringMVC 4.1.6,CXF 3.1.0项目管理:apache-maven-3.3.3 pom.xml <project x ...
随机推荐
- caffe的python接口学习(5)生成deploy文件
如果要把训练好的模型拿来测试新的图片,那必须得要一个deploy.prototxt文件,这个文件实际上和test.prototxt文件差不多,只是头尾不相同而也.deploy文件没有第一层数据输入层, ...
- 不就是语法和长难句吗—笔记总结Day3
♦5♦状语从句——结果状语从句 · so(+adj / adv)...that · such(+ n)...that ♦6♦状语从句——让步状语从句 · although · though · eve ...
- 洛谷 P3916 【图的遍历】
这道题绿题有点高了吧... 我一开始的思路就是一个暴力的遍历,用递归加一个记忆化,对于一个点不断的往下搜索,然后确定最大的,返回,给上面的节点.就在这个过程中,我们是搜到最大的数然后返回给上层的数,那 ...
- \\u4e00-\\u9fa5\
select * from stu where name regexp '[\\u4e00-\\u9fa5\·]{2,10}$'; 结果: name这个字段从后到前 2 到10个字符之内 如果有汉字 ...
- 泊车SLAM文献整理
1. 泊车: 线车位检测 Geometric Features-Based Parking Slot Detection 译文链接:https://blog.csdn.net/djfjkj52/art ...
- MVC + EFCore 项目实战 - 数仓管理系统2- 搭建基本框架配置EFCore
本次课程就正式进入开发部分. 首先我们先搭建项目框架,还是和之前渐进式风格保持一致,除必备组件外,尽量使用原生功能以方便大家理解. 开发工具:vs 2019 或以上 数据库:SQL SERVER 20 ...
- REST,RPC和GraphQL应用场景,WebHooks、WebSocket、HTTP Streaming应用场景。
一.请求--响应API. 请求--响应类的API的典型做法是,通过基于HTTP的Web服务器暴露一个/套接口.API定义一些端点,客户端发送数据的请求到这些端点,Web服务器处理这些请求,然后返回响应 ...
- SpringBoot常用数据源配置
#mysql8.X url: jdbc:mysql://localhost:3306/yourdbname?serverTimezone=UTC&useSSL=false&allowP ...
- SQL批量插入数据【万级】
1.每4000条插入一次 for (int i = 0; i < dt.Rows.Count; i++) { IsTBProductForStockInfo model = new IsTBPr ...
- 蜂鸟E203系列——Linux下运行hello world例程
欲观原文,请君移步 创建程序 在 -/hbird-e-sdk-master/software 路径下创建一个"helloworld"中文件夹 在 -/hbird-e-sdk-mas ...