springboot+CXF开发webservice对外提供接口(转)
文章来源:http://www.leftso.com/blog/144.html
1.项目要对外提供接口,用webservcie的方式实现
2.添加的jar包
maven:
- <dependency>
- <groupId>org.apache.cxf</groupId>
- <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
- <version>3.1.7</version>
- </dependency>
gradle:
- compile group: 'org.apache.cxf', name: 'cxf-spring-boot-starter-jaxws', version: '3.1.7'
3.接口类
- package com.webservice.test;
- import javax.jws.WebMethod;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- /**
- * Created by admin on 2017/5/9 15:02.
- */
- @WebService(name = "ITestCollect", // 服务名称
- targetNamespace = "http://test.webservice.com/"// 命名空间,一般是接口的包名倒序
- )
- public interface ITestCollect {
- @WebMethod //@webMethod:方法注解,表示暴露的方法 @webParam:参数注解,name:参数名称
- String test(@WebParam(name = "name") String name);
- }
实现类
- package com.webservice.test;
- import org.springframework.stereotype.Service;
- import javax.jws.WebService;
- /**
- * Created by admin on 2017/5/9 15:02.
- */
- @Service
- @WebService(serviceName = "ITestCollect",//与接口中name一致
- targetNamespace = "http://test.webservice.com/",//与接口中的 targetNamespace 属性一致
- endpointInterface = "com.webservice.test.ITestCollect"//接口的全路径
- )
- public class TestCollectImpl implements ITestCollect {
- @Override
- public String test(String name) {
- System.out.println("name:" + name);
- return "success:"+name;
- }
- }
配置config
- package com.webservice.test;
- import com.webservice.test.ITestCollect;
- 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;
- /**
- * Created by admin on 2017/5/9 14:40.
- */
- @Configuration
- public class WebServiceConfig {
- @Autowired
- private Bus bus;
- @Autowired
- ITestCollect testCollect;
- @Bean
- public Endpoint endpoint() {
- EndpointImpl endpoint = new EndpointImpl(bus, testCollect);
- endpoint.publish("/testCollect");
- return endpoint;
- }
- }
配置完成后 启动springboot 直接访问 http://localhost:8080/services/testCollect?wsdl
可以看到一个xml的页面,则部署成功
/services/ 是固定的,/testCollect 是代码里设置的路径 ?wsdl 是固定的
这里有一个坑:
(1)例子中的所有类都是在一个包下面的,要是在不同的包下面,会有各种错,就是找不到对应的类,可能要配置 targetNamespace 属性或者其参数,没有研究,但是放在同一个包下面就没问题,
(2)调用完成后,返回值是对象或者是list类型,方法上不能加 @WebResult 注解,不然要在对应的类上面写上实体类转xml的注解,新建的实体类也要在同一个包下,不然返回的对象找不到包路径
(3)cxf不支持重载,如果接口中有重载方法,要重命名 @WebMethod(operationName = "xxxx") operationName属性是重新取名
4.调用
- package com.webservice.test;
- import com.webservice.test.ITestCollect;import org.apache.cxf.endpoint.Client;
- import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
- import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
- import java.util.List;
- /**
- * Created by admin on 2017/5/9 20:36.
- */
- public class WebServiceTest {
- public static void main(String[] args) {
- test2();
- }
- /**
- * 方式1.代理类工厂的方式,需要拿到对方的接口
- */
- public static void test1() {
- try {
- String address = "http://localhost:8080/services/testCollect?wsdl";
- JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
- jaxWsProxyFactoryBean.setAddress(address);
- jaxWsProxyFactoryBean.setServiceClass(ITestCollect.class);
- ITestCollect cs = (ITestCollect) jaxWsProxyFactoryBean.create();
- String userName = "test";
- String result = cs.test(userName);
- System.out.println("返回结果:" + result);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 方式2.动态调用方式
- */
- public static void test2() {
- // 创建动态客户端
- JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
- Client client = dcf.createClient("http://localhost:8080/services/testCollect?wsdl");
- Object[] objects = new Object[0];
try {- //用法:client.invoke("方法名",参数1,参数2,参数3....);
- // objects = client.invoke("add", model);
- objects = client.invoke("test");
- System.out.println("success.....");
- System.out.println("返回数据:" + objects[0]);
- } catch (java.lang.Exception e) {
- e.printStackTrace();
- }
- }
- }
文章来源:http://www.leftso.com/blog/144.html
springboot+CXF开发webservice对外提供接口(转)的更多相关文章
- springboot+cxf 开发webservice
参考 https://www.cnblogs.com/fuxin41/p/6289162.html pom.xml <?xml version="1.0" encoding= ...
- 使用cxf开发webservice接口
项目中经常用到开发webservice接口,及调用webService接口.这里讲解如何使用cxf开发webService接口. 一.webservice介绍及理解 webservice是一种跨平台, ...
- 开发FTP服务接口,对外提供接口服务
注意:本文只适合小文本文件的上传下载,因为post请求是有大小限制的.默认大小是2m,虽然具体数值可以调节,但不适合做大文件的传输 最近公司有这么个需求:以后所有的项目开发中需要使用ftp服务器的地方 ...
- 【WebService】使用CXF开发WebService(四)
CXF简介 Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.CXF 继承了 Celtix ...
- struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
Cxf + Spring+ myeclipse+ cxf 进行 Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...
- 使用WCF对外提供接口
本篇将通过WCF以webservices的方式对外提供接口.同时使用NUnit对webservices中的方法进行单元测试. 开发契约 contract Contract项目为类库项目,该项目下会包含 ...
- 使用cxf开发webservice应用时抛出异常
在使用cxf开发webservice应用时,报出了类似下面的错误 JAXB: [javax.xml.bind.UnmarshalException: unexpected element (uri:& ...
- Frp内网穿透搭建,家庭主机对外提供接口,支持ssh访问
Frp内网穿透搭建,家庭主机对外提供接口,支持ssh访问 1.使用场景: 需求1.家中服务器 ubuntu 主机,跑接口服务,需要对外暴漏, 需求2.同时需要在外网ssh远程 关键词: frp内网 ...
- CXF 开发 WebService
什么是CXF: Apache CXF = Celtix + Xfire 支持多种协议: SOAP1.1,1.2 XML/HTTP CORBA(Common Object Request Broker ...
随机推荐
- select * from 的一些心得
如何简单运用好 select * from语句,在不同的函数下,有不同的先后顺序. 语法格式 (代表先后顺便) (2)select 字段名 查询什么东西 (1)from 表名,从哪个表查询 例如:查询 ...
- Python 面向对象(一) 基础
Python 中一切皆对象 什么是面向对象? 面向对象就是将一些事物的共有特征抽象成类,从类来创建实例. 类class 可以理解为模版 比如人类,都具有身高.体重.年龄.性别.籍贯...等属性,但属性 ...
- LKD: Chapter 8 Bottom Halves and Deferring Work
In 2.6.x, there are 3 mechanisms for implementing a bottom half: softirqs, tasklets and work queues. ...
- unity android相互调用
简介 有一些手机功能,Unity没有提供相应的接口,例如震动,例如不锁屏,例如GPS,例如... 有太多的特殊功能Unity都没有提供接口,这时候,我们就需要通过使用Android原生的ADT编辑器去 ...
- Bootstrap-datepicker3官方文档中文翻译---概述(原版翻译 http://bootstrap-datepicker.readthedocs.io/en/latest/index.html)
bootstrap-datepicker Bootstrap-datepicker 提供了一个拥有Bootstrap样式的弹性Datepicker控件 Requirements/使用要求 Bootst ...
- Axios 执行post发送两次请求的小坑
vue-resource2.0已经不再更新,所以vue2.0官方推荐使用axios来代替.实际项目也是应用上了vue+axios,然后就有了这么一段填坑的经历. 问题:axios使用post请求时,发 ...
- Spring Cloud Ribbon 整合 Hystrix
在前面随笔 Spring Cloud 之 Ribbon 的ribbon工程基础上进行改造 1.pom.xml 加入依赖 <dependency> <groupId>org.sp ...
- 51Nod 1284 2 3 5 7的倍数 容斥原理
1284 2 3 5 7的倍数基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注给出一个数N,求1至N中,有多少个数不是2 3 5 7的倍数. 例如N = 1 ...
- PAT 1008. Elevator (20)
1008. Elevator (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The highest ...
- 使用superMap实现点标注和区域着色
1.定义html文件,引入superMap的js和theme文件: <script src='${_ctxPath }/statics/js/superMap/SuperMap.Include. ...