Apache CXF是一个开源的WebService RPC框架。

例子:

1. 新建一个maven web项目, 添加pom

如下:

<?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.genesis.architect</groupId><artifactId>cxf-demo</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><java.version>1.8</java.version><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><cxf.version>3.1.7</cxf.version><spring.version>4.0.9.RELEASE</spring.version><junit.version>4.12</junit.version><jstl.version>1.2</jstl.version><servlet-api.version>2.5</servlet-api.version><jsp-api.version>2.0</jsp-api.version></properties><!-- pom.xml增加多环境配置的配置 --><profiles><profile><id>dev</id><activation><activeByDefault>true</activeByDefault></activation><properties><package.environment>development</package.environment></properties></profile><profile><id>pro</id><properties><package.environment>production</package.environment></properties></profile><profile><id>test</id><properties><package.environment>testing</package.environment></properties></profile></profiles><dependencies><!-- spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>${cxf.version}</version></dependency><!-- servlet start --><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>${jstl.version}</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>${servlet-api.version}</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jsp-api</artifactId><version>${jsp-api.version}</version><scope>provided</scope></dependency><!-- servlet end -->
</dependencies><build><finalName>cxf-demo</finalName><resources><resource><directory>src/main/resources/${package.environment}</directory><includes><include>**/*.properties</include><include>**/*.xml</include><include>**/*.tld</include></includes><filtering>false</filtering></resource><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xml</include><include>**/*.tld</include></includes><filtering>false</filtering></resource></resources><plugins><!-- Jetty 服务器 --><plugin><groupId>org.eclipse.jetty</groupId><artifactId>jetty-maven-plugin</artifactId><version>9.2.2.v20140723</version><configuration><httpConnector><port>8080</port></httpConnector><webAppConfig><contextPath>/${project.artifactId}</contextPath></webAppConfig><scanIntervalSeconds>2</scanIntervalSeconds></configuration></plugin><!-- tomcat 服务器 --><!--<plugin>--><!--<groupId>org.apache.tomcat.maven</groupId>--><!--<artifactId>tomcat7-maven-plugin</artifactId>--><!--<configuration>--><!--<port>8080</port>--><!--<path>/${project.artifactId}</path>--><!--</configuration>--><!--</plugin>--><!--打war包到指定的目录下 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>2.8</version><executions><execution><id>copy-war</id><phase>package</phase><goals><goal>copy</goal></goals><configuration><artifactItems><artifactItem><groupId>${project.groupId}</groupId><artifactId>${project.artifactId}</artifactId><version>${project.version}</version><type>${project.packaging}</type></artifactItem></artifactItems><!-- <outputDirectory>${dist.console.war.dir}</outputDirectory> --><!--指定war包保存地址 --><outputDirectory>${basedir}/WAR/${package.environment}</outputDirectory></configuration></execution></executions></plugin></plugins></build>
</project>

2. 定义远程服务接口,并用@WebService标明是一个远程的WebService

package com.genesis.architect.cxf.service;import javax.jws.WebService;/*** @author KG created on 16/12/4*/
@WebService
public interface HelloService {public String sayHello(String content);}

3. 远程服务的实现(通过endpointInterface指明对应的接口)

package com.genesis.architect.cxf.service;import javax.jws.WebService;/*** @author KG created on 16/12/4*/
@WebService(endpointInterface = "com.genesis.architect.cxf.service.HelloService")
public class HelloServiceImpl implements HelloService {@Overridepublic String sayHello(String content) {return "hello," + content;}
}

4. 配置WebService的终结点实现,并配置地址映射

cxf-server.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><import resource="classpath:META-INF/cxf/cxf.xml"/><import resource="classpath:META-INF/cxf/cxf-servlet.xml"/><jaxws:endpoint id="helloWorld" implementor="com.genesis.architect.cxf.service.HelloServiceImpl" address="/HelloWorld"/></beans>

5. web.xml配置WebService监听,添加ws路由映射

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>cxf-spring</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:cxf-server.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>CXFServlet</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>CXFServlet</servlet-name><url-pattern>/ws/*</url-pattern></servlet-mapping></web-app>

6. 编写客户端测试

package com.genesis.architect.cxf.client;import com.genesis.architect.cxf.service.HelloService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @author KG created on 16/12/4*/
public class CxfClient {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("classpath:cxf-client.xml");HelloService client = (HelloService) context.getBean("helloClient");System.out.println(client.sayHello("Master HaKu"));}}

7. 客户端调用远程服务的配置文件(cxf-client.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:jaxws="http://cxf.apache.org/jaxws"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><jaxws:client id="helloClient"serviceClass="com.genesis.architect.cxf.service.HelloService"address="http://localhost:8080/cxf-demo/ws/HelloWorld" />
</beans>

8. 运行结果

访问:

http://localhost:8080/cxf-demo/ws/HelloWorld?wsdl

可以看到soap的wsdl内容

运行client:

hello,Master HaKu

转载于:https://www.cnblogs.com/davidgu/p/8566088.html

分布式架构探索 - 2. WebService RPC框架之Apache CXF相关推荐

  1. 【分布式服务架构】常用的RPC框架

    1. RPC 框架的原理 RPC(Remote Procedure Call,远程服务调用),用来实现部署在不同机器之间系统的方法调用,使程序像当问本地系统资源一样,通过网络传出资源. 1)Clien ...

  2. 分布式架构-ZK客户端工具Curator框架分布式锁及基本使用

    分布式架构-基于Curator分布式锁及基本使用 一.Curator Curator是Netflix公司开源的一套zookeeper客户端框架,解决了很多Zookeeper客户端非常底层的细节开发工作 ...

  3. springboot版本升级导致webservice调用失败org.apache.cxf.common.jaxb.JAXBUtils.createMininumEscapeHandle

    很感谢这位作者的文章https://blog.csdn.net/q340505050518/article/details/105394315 近期对项目版本进行升级 原项目版本 从 Springcl ...

  4. @webservice报错org.apache.cxf.common.i18n.UncheckedException: No operation was found with

    文章目录 1. 现象 2. 解决办法1 3. 解决办法2 1. 现象 整合spring+cxf的webservice,成功发布了wsdl,但在调用的时候报错 org.apache.cxf.common ...

  5. 什么是RPC?RPC框架dubbo的核心流程

    一.REST 与 RPC: 1.什么是 REST 和 RPC 协议: 在单体应用中,各模块间的调用是通过编程语言级别的方法函数来实现,但分布式系统运行在多台机器上,一般来说,每个服务实例都是一个进程, ...

  6. 经典项目|手撸一个高质量RPC框架

    hi, 大家好,RPC是后端系统节点之间通信的核心技术,属于后端开发必须要学习的技能. 后端技术趋势指南|如何选择自己的技术方向 如何从0搭建公司的后端技术栈 远程过程调用(Remote Proced ...

  7. 一文探讨 RPC 框架中的服务线程隔离

    Kirito 推荐语:最近秋招开始了,很多学生开始准备起了秋招,有很多人想知道进一些有名的互联网公司实习有什么要求,正好最近跟一位阿里春招的实习小伙子聊了一些 RPC 相关的知识点,于是我把这篇他的思 ...

  8. 调用线程必须为sta_Java手写分布式系统远程调用RPC框架

    一.RPC简介 最近看hadoop底层通信,都是通过RPC实现的. RPC(Remote Procedure Call Protocol)远程调用: 远程过程调用是一种常用的分布式网络通信协议,它允许 ...

  9. WebService开发笔记 1 -- 利用cxf开发WebService竟然如此简单

    现在的项目中需要用到SOA概念的地方越来越多,最近我接手的一个项目中就提出了这样的业务要求,需要在.net开发的客户端系统中访问java开发的web系统,这样的业务需求自然需要通过WebService ...

最新文章

  1. C++:随笔8---命名空间
  2. ionic 获取input的值
  3. 关闭eslint检验;vue-cli3搭建的vue项目关闭eslint;脚手架3关闭eslint;
  4. 第二节 CSS入门介绍
  5. 零窗口探测怎么抓包_万事俱备,只待“窗口”!航天任务中的重要环节:“发射窗口”!...
  6. Android 滑动定位+吸附悬停效果实现
  7. js修改IOS微信title
  8. 推荐一款专为新手用的Python开发工具
  9. linux变utf8为sjis命令,在派上编译Linux版Onscripter-jh时出现问题
  10. 用acdsee制作html,ACDSee 制作网络像册
  11. 基于四叉树的图像压缩问题
  12. 【源码汇总】基于RGB-D相机的三维重建总览 包括静态与动态三维重建
  13. 树莓派介绍树莓派3代B+型开发板
  14. 库卡机器人坐标手势_库卡机器人为何要几种坐标系?
  15. 整理: 显示面板行业英文简称解析
  16. English and Programming_Day1
  17. 最简单的h264/h265/svac和g711封装成ps流符合gb28181过检码流要求
  18. 计算机视觉中的Transformer
  19. 程序物语(七):项目经理预成长
  20. 移动测试基础 Android 应用测试总结

热门文章

  1. 不懂管理,你拿什么赢别人!商业奇才10句话,老板奉为管理圣经
  2. 谷歌、DeepMind强强联手再发布Dreamer:性能远超“前辈”PlaNet
  3. 微软分享史上最大基于Transformer架构的语言生成模型
  4. 神经进化:一种不一样的深度学习
  5. 挡不住的AI成熟趋势
  6. (超级详细)jit的介绍和用法
  7. 20210709未来智能实验室收录资料
  8. 费米悖论的三十种解释
  9. 薛其坤院士对话马斯克:下一个颠覆性创新是什么?
  10. 什么是内卷?华为内部这篇文章读懂