文章来源: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对外提供接口(转)的更多相关文章

  1. springboot+cxf 开发webservice

    参考 https://www.cnblogs.com/fuxin41/p/6289162.html pom.xml <?xml version="1.0" encoding= ...

  2. 使用cxf开发webservice接口

    项目中经常用到开发webservice接口,及调用webService接口.这里讲解如何使用cxf开发webService接口. 一.webservice介绍及理解 webservice是一种跨平台, ...

  3. 开发FTP服务接口,对外提供接口服务

    注意:本文只适合小文本文件的上传下载,因为post请求是有大小限制的.默认大小是2m,虽然具体数值可以调节,但不适合做大文件的传输 最近公司有这么个需求:以后所有的项目开发中需要使用ftp服务器的地方 ...

  4. 【WebService】使用CXF开发WebService(四)

    CXF简介 Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.CXF 继承了 Celtix ...

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

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

  6. 使用WCF对外提供接口

    本篇将通过WCF以webservices的方式对外提供接口.同时使用NUnit对webservices中的方法进行单元测试. 开发契约 contract Contract项目为类库项目,该项目下会包含 ...

  7. 使用cxf开发webservice应用时抛出异常

    在使用cxf开发webservice应用时,报出了类似下面的错误 JAXB: [javax.xml.bind.UnmarshalException: unexpected element (uri:& ...

  8. Frp内网穿透搭建,家庭主机对外提供接口,支持ssh访问

    Frp内网穿透搭建,家庭主机对外提供接口,支持ssh访问 1.使用场景: 需求1.家中服务器 ubuntu 主机,跑接口服务,需要对外暴漏, 需求2.同时需要在外网ssh远程 ​ 关键词: frp内网 ...

  9. CXF 开发 WebService

    什么是CXF: Apache CXF = Celtix + Xfire 支持多种协议: SOAP1.1,1.2 XML/HTTP CORBA(Common Object Request Broker ...

随机推荐

  1. Docker Swarm 中最重要的概念- 每天5分钟玩转 Docker 容器技术(94)

    从主机的层面来看,Docker Swarm 管理的是 Docker Host 集群.所以先来讨论一个重要的概念 - 集群化(Clustering). 服务器集群由一组网络上相互连接的服务器组成,它们一 ...

  2. 发现AspNet.Core版本控制库Bug一枚,你还想入坑?

    我,博客写作小白一枚,注册账号多年却未曾留下只言片语,在潜水的这些年里从大家的博客中收获了很多新的知识忽觉惶恐心有不安,是时候给大家分享一些我的经验和教训了.嗯嗯,实话告诉大家前面的话的都是来凑字数的 ...

  3. Java面试之框架篇(八)

    71,谈谈你对Struts的理解. 1. struts是一个按MVC模式设计的Web层框架,其实它就是一个Servlet,这个Servlet名为ActionServlet,或是ActionServle ...

  4. 老男孩Python全栈开发(92天全)视频教程 自学笔记01

    day1课程目录: 开课介绍(1) 开课介绍(2) 开课介绍(3) 电脑简史(1) 电脑简史(2) 计算机结构 day1课程内容梳理: 导师介绍: Alex Li(金角大王):买了一辆特斯拉,喜欢姑娘 ...

  5. ecshop根据订单号查询物流信息

    目标:订单详情页可以根据订单查询当前物流信息. 效果图: 思路:点击后异步请求快递查询api,接受返回信息,拼接. 代码: admin下:order_info.htm //一:顶部插入jquery,在 ...

  6. ASP.NET中HttpContext.Cache的使用

    -------------------------------键 --值-----依赖-----过期时间-------------------------------绝对过期------------- ...

  7. 开源API测试工具 Hitchhiker v0.5更新 - 完善细节

    Hitchhiker 是一款开源的支持多人协作的 Restful Api 测试工具,支持Schedule, 数据对比,压力测试,支持上传脚本定制请求,可以轻松部署到本地,和你的team成员一起管理Ap ...

  8. 08-图8 How Long Does It Take

    原题: Given the relations of all the activities of a project, you are supposed to find the earliest co ...

  9. selenium 执行js,实现滚动条

    今天在写脚本的时候,学习了执行js,实现滚动条,对于scrollTop=10000中这个10000是怎么来的,还不是很了解,先将方法记录一下, 1.滚动条回到顶部: js_up="docum ...

  10. WebApi接收复杂类型参数

    当接收实体时,该实体类不能添加Serializable属性,否则传来的json数据无法映射成功?