spring boot整合cxf发布webservice服务和cxf客户端调用
来源:互联网 发布:无忧seo 编辑:程序博客网 时间:2024/06/02 05:00
spring boot整合cxf发布webservice服务和cxf客户端调用
本案例使用maven方式
核显文件清单
1.pom.xml
2.CommonService.java 服务接口
3.接口实现
4.配置cxf服务发布,默认服务在Host:port/services/***路径下
这里相当于把Commonservice接口发布在了路径/services/CommonService下,wsdl文档路径为
http://localhost:8080/services/CommonService?wsdl
创建基于cxf的客服端调用webservice接口(非使用wsdl文档生成java类)
调用后返回结果输出为
Hello,Leftso
项目源码GITHUB下载:点击下载
本案例使用maven方式
核显文件清单
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.leftso</groupId><artifactId>demo-webservice-cxf</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>demo-webservice-cxf</name><description>Demo project for Spring Boot security</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.5.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- CXF webservice --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.1.11</version></dependency><!-- CXF webservice --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
2.CommonService.java 服务接口
package com.leftso.webservice;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;/** * 接口 * * @author leftso * */@WebService(name = "CommonService", // 暴露服务名称targetNamespace = "http://webservice.leftso.com/"// 命名空间,一般是接口的包名倒序)public interface CommonService {@WebMethod@WebResult(name = "String", targetNamespace = "")public String sayHello(@WebParam(name = "userName") String name);}
3.接口实现
package com.leftso.webservice;import javax.jws.WebService;import org.springframework.stereotype.Component;/** * 接口实现 * * @author leftso * */@WebService(serviceName = "CommonService", // 与接口中指定的name一致targetNamespace = "http://webservice.leftso.com/", // 与接口中的命名空间一致,一般是接口的包名倒endpointInterface = "com.leftso.webservice.CommonService"// 接口地址)@Componentpublic class CommonServiceImp implements CommonService {@Overridepublic String sayHello(String name) {return "Hello ," + name;}}
4.配置cxf服务发布,默认服务在Host:port/services/***路径下
package com.leftso.config;import javax.xml.ws.Endpoint;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 com.leftso.webservice.CommonService;@Configurationpublic class CxfConfig {@Autowiredprivate Bus bus;@AutowiredCommonService commonService;/** JAX-WS **/@Beanpublic Endpoint endpoint() {EndpointImpl endpoint = new EndpointImpl(bus, commonService);endpoint.publish("/CommonService");return endpoint;}}
这里相当于把Commonservice接口发布在了路径/services/CommonService下,wsdl文档路径为
http://localhost:8080/services/CommonService?wsdl
创建基于cxf的客服端调用webservice接口(非使用wsdl文档生成java类)
package com.leftso.client;import org.apache.cxf.endpoint.Client;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;import com.leftso.webservice.CommonService;public class CxfClient {public static void main(String[] args) {cl1();}/** * 方式1.代理类工厂的方式,需要拿到对方的接口 */public static void cl1() {try {// 接口地址String address = "http://localhost:8080/services/CommonService?wsdl";// 代理工厂JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();// 设置代理地址jaxWsProxyFactoryBean.setAddress(address);// 设置接口类型jaxWsProxyFactoryBean.setServiceClass(CommonService.class);// 创建一个代理接口实现CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();// 数据准备String userName = "Leftso";// 调用代理接口的方法调用并返回结果String result = cs.sayHello(userName);System.out.println("返回结果:" + result);} catch (Exception e) {e.printStackTrace();}}/** * 动态调用方式 */public static void cl2() {// 创建动态客户端JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();Client client = dcf.createClient("http://localhost:8080/services/CommonService?wsdl");// 需要密码的情况需要加上用户名和密码// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,// PASS_WORD));Object[] objects = new Object[0];try {// invoke("方法名",参数1,参数2,参数3....);objects = client.invoke("sayHello", "Leftso");System.out.println("返回数据:" + objects[0]);} catch (java.lang.Exception e) {e.printStackTrace();}}}
调用后返回结果输出为
Hello,Leftso
项目源码GITHUB下载:点击下载
http://www.leftso.com/blog/144.html
阅读全文
0 0
- spring boot整合cxf发布webservice服务和spring boot整合cxf客户端调用webservice服务
- spring boot整合cxf发布webservice服务和cxf客户端调用
- spring boot整合cxf发布webservice服务和cxf客户端调用
- cxfspring boot整合cxf发布webservice服务和cxf客户端调用
- (四)CXF整合Spring发布WebService服务
- Spring整合CXF之发布WebService服务
- Spring整合CXF之发布WebService服务
- Spring整合CXF之发布WebService服务
- Spring整合CXF配置WebService服务发布
- Spring整合CXF之发布WebService服务
- cxf+spring发布webservice服务(客户端)
- CXF实现和Spring整合发布WebService
- Spring整合CXF发布WebService
- cxf整合spring发布webservice
- CXF 发布WebService - 客户端调用
- cxf+spring发布webservice和调用
- cxf+spring发布webservice和调用
- Spring整合CXF,发布RSETful 风格WebService--编写客户端代码,调用RESTful WebService
- Linux history显示时间
- 浅谈CSRF攻击方式
- Python基础——list.append() 与 list.extend() 的区别
- Codeforces 830A, Binary Search
- hibernate criteria一对多查询,并对多方的集合数据进行过滤,返回部分集合
- spring boot整合cxf发布webservice服务和cxf客户端调用
- 深浅拷贝
- Java中Map, List, Set和Queue的区别和使用场景
- PYTHON爬虫学习-糗事百科内容爬取
- 1:分析菱形继承的问题。 2:剖析虚继承是怎么解决二义性和数据冗余的。
- 深入理解linux内核——内存寻址
- Windows 10右键点击Word EXCEL PPT PDF等,系统资源管理器立即重启解决方法
- 虚拟机CentOS7 配置固定IP地址
- 微信小程序列表的上拉加载和下拉刷新的实现