文章来源: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. python基础阶段 经典练习题 拾英札记(3)

    对于编程学习来说,动手操练和重复训练很重要. 因为这是一个注重实践的活,最终要下笔落字. 更何况,即使你看了很多博客,听了很多课,你脑中的认识和手指下的-屏幕上的反馈,逻辑上是两个维度-两个载体的,中 ...

  2. Spring 4 MVC example with Maven

    In this tutorial, we show you a Spring 4 MVC example, using Maven build tool. Technologies used : Sp ...

  3. 如何创建 Swarm 集群?- 每天5分钟玩转 Docker 容器技术(95)

    本节我们将创建三节点的 swarm 集群. swarm-manager 是 manager node,swarm-worker1 和 swarm-worker2 是 worker node. 所有节点 ...

  4. WebService的基本介绍

    一.WebService的基本介绍    1.WebService是什么? WebService ---> Web Service web的服务    2.思考问题: WebService是we ...

  5. 巧学DBhelper

    这几天在教我很重要的人学习,她属于那种超级小白,很超级的那种. 教她的过程中 发现有的知识点 不管这么教都不会.DBhelper就是不知道怎么记. 当时我就想到 杰哥(程杰)的出的大话系列,和他写书的 ...

  6. BST 解析 (一)

    这篇博文主要初步介绍Binary Search Tree(BST)的一些基本功能以及应用场景,由于BST的相关知识比较多,下一节会接着补充BST的一些功能.这一节主要分为以下三个要素: BST 的定义 ...

  7. Less的@import指令

    Less的@import指令 Less中,可以通过 @import指令来导入外部文件.@import指令可以放在代码中的任何位置,导入文件时的处理方式取决于文件的扩展名: 如果扩展名是 .css,文件 ...

  8. Class 与 Style 绑定

    将 v-bind 用于 class 和 style 时,Vue.js 做了专门的增强.表达式结果的类型除了字符串之外,还可以是对象或数组. 绑定 HTML Class 对象语法 <div cla ...

  9. Python测试开发之函数

    对于初学者而言,感觉函数还是不是很好理解,尤其是当写一个脚本,或者是写一个算法,认为可能for循环就已经可以解决的问题为什么还要用函数来实现呢? 今天就来说一下函数的优点,其实函数的最大优点就是可重用 ...

  10. Python测试开发之---string

    string.letters 所有的大小写字母 >>> print string.lettersabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQ ...