参考官网文档:http://cxf.apache.org/docs/writing-a-service-with-spring.html

从官网上下载 cxf 的包,包里会有 samples 文件夹,该文件夹中存放的就是cxf 的一些小例子

这里就是针对 java_first_spring_support 例子的改写 与 说明,该例子是采用 spring +maven +cxf 技术

创建项目

  • 使用Eclipse 创建一个 Maven Project,工程名为 TestCXFDome1 ,修改pom.xml 引入需要使用的jar 包
<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>panie</groupId><artifactId>TestCXFDome1</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>TestCXFDome1 Maven Webapp</name><url>http://maven.apache.org</url><properties><cxf.version>3.1.6</cxf.version><springframework.version>4.1.9.RELEASE</springframework.version></properties><dependencies><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.6</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.1.6</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.2</version><type>jar</type></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version><type>jar</type></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.1</version><scope>provided</scope></dependency></dependencies><build><finalName>TestCXFDome1</finalName></build>
</project>

编写服务端

  • 首先写一个接口。@WebService注解表示是要发布的web服务
package demo.spring.service;import javax.jws.WebService;@WebService
public interface HelloWorld {String sayHi(String text);
}

  • 对该接口写一个实现类。@WebService注解表示是要发布的web服务,endpointInterface参数的值是该服务类对应的接口。
package demo.spring.service;import javax.jws.WebService;@WebService(endpointInterface = "demo.spring.service.HelloWorld")
public class HelloWorldImpl implements HelloWorld {public String sayHi(String text) {System.out.println("sayHi called");return "Hello " + text;}
}

  • 声明 server bean

  CXF 支持 Spring 2.0 Schema 标签配置方式,并且提供快捷暴露 Web Services 的标签。对于 JAX_WS 端,我们可以使用<jaxws:endpoint> 来设置服务器端的 endpoint (请参考英文原文)

  1)我们需要引入 Spring 与 CXF 的命名空间(namespace),这样,我们可以使用 Spring 与 CXF 的标签配置了。  2)我们需要引入我们所需要的 CXF 的 Bean 定义文件

  3)定义我们具体实现的 Bean

  在 WEB-INF 目录下创建一个 cxf-servlet.xml 文件来声明 一个endpoint bean

<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="demo.spring.service.HelloWorldImpl" address="/HelloWorld"/>
</beans>

  如果 想引入 spring 来管理bean,可以这样写

<bean id="hello" class="demo.spring.service.HelloWorldImpl" /><jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />

  id - 指在 spring context 中的bean id

  implementor - 指定 实现类。当使用bean 来表示实现类时,使用 #bean-name

  address - 指 服务将会被发布的位置,这里只能使用相对路径。因为CXF 不知道 war 名字,也不知道 容器端口,CXF 运行时将会更新 endpoint 地址

  • 配置Servlet

  如果采用 默认路径的 cxf-servlet.xml ,在web.xml 中配置

    <servlet><description>Apache CXF Endpoint</description><display-name>cxf</display-name><servlet-name>cxf</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>cxf</servlet-name><url-pattern>/*</url-pattern></servlet-mapping>

  如果 cxf-servlet.xml  更换的位置 或者 名字,如改为 beans.xml ,则需要在 web.xml 中配置

<context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:/beans.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

部署到web服务器上,发布webservice工程,输入http://localhost:8080/TestCXFDome1/HelloWorld?wsdl

编写客户端(一)

  在客户端可以使用 <jaxws:client> 来声明。你只需要提供 bean name,服务接口,服务请求URL,它就可以在远程SOAP服务器上创建一个指定名称的bean来实现服务接口。

  • 配置一个 client-bean.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/schema/jaxws.xsd"><bean id="client" class="demo.spring.service.HelloWorld" factory-bean="clientFactory" factory-method="create"/><bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"><property name="serviceClass" value="demo.spring.service.HelloWorld"/><property name="address" value="http://localhost:8080/TestCXFDome1/HelloWorld"/></bean>
</beans>

  • 编写一个 测试类 Client
package demo.spring.client;import org.springframework.context.support.ClassPathXmlApplicationContext;import demo.spring.service.HelloWorld;public final class Client {private Client() {}public static void main(String args[]) throws Exception {// START SNIPPET: client
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});HelloWorld client = (HelloWorld)context.getBean("client");String response = client.sayHi("Joe");System.out.println("Response: " + response);System.exit(0);// END SNIPPET: client
    }
}

运行 测试类,即可看到 调用 服务的结果

编写客户端(二)

package demo.spring.client;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import demo.spring.service.HelloWorld;public class Client2
{public static void main(String[] args){//创建WebService客户端代理工厂JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();//注册WebService接口factory.setServiceClass(HelloWorld.class);//设置WebService地址factory.setAddress("http://localhost:8180/TestCXFDome1/HelloWorld");HelloWorld hello = (HelloWorld)factory.create();//调用webservice接口方法String response = hello.sayHi("panie");//返回sucessSystem.out.println("Response: " + response);}
}

使用CXF 来发布一个 service相关推荐

  1. Web Service (二) CXF自动发布Web Service(No Spring)

    Web Service实现目前流行的框架主要有两种,cxf和axis这两个框架,下面是这两个框架的优缺点,我们这个项目中使用的是cxf这个框架,首先看一下没有集成spring的时候是怎么实现远程调用的 ...

  2. 如何在Mendix中发布一个REST Service

    目录 介绍(Introduction) REST是什么? Published REST Service 一般属性 Service Name(服务名称) Version (版本) Location 位置 ...

  3. CXF:是一个开源Service框架支持多种协议:SOAP、XML/HTTP、RestfulHTTP和CORBA,同时可以和Spring集成。

    CXF:是一个开源Service框架 支持多种协议:SOAP.XML/HTTP.RestfulHTTP和CORBA,同时可以和Spring集成.

  4. Maven项目集成cxf框架发布WebService

    关于Maven项目集成cxf框架发布和接收WebService 从网上找了很多,发现大多数都是类似"单机"版的发布,直到看了一篇博客,给我很大的启发. 在此感谢这位博客的作者:ht ...

  5. Android开发中调用Spring CXF整合发布的WebService接口为什么抛出异常错误?

    摘要:最近在协助同事搞Android调用WebService接口,再测试的过程中发现老师报错,经过baidu,google,终于解决了,现在记录一下: 一:错误信息: 2015-10-28 18:50 ...

  6. Tuscany SCA 发布Web Service

    有段时间没有学习SOA了,最近偶然碰到一个例子,运行修改理解后发布到此. 在前面的博客中有提到用axis2发布web service,借用了插件,并且步骤较繁多,现在来看下tuscany简单地通过配置 ...

  7. 创建构建方法android,如何快速创建并发布一个 Android 库

    一. 前言 最近经常看到各种大神的库,发现用起来非常方便,自己研究了一下,来写个库发布一下,让自己写代码更加方便一点,自己封装了基本的开发工具类.也是搜集了各位大神的优秀代码总结的. 二.必要的准备工 ...

  8. 发布一个JINI服务

    这一篇文章讲解如何发布一个JINI的服务. (参考的书是:JINI EXAMPLE BY EXAMPLE) 1.提供一个服务接口 首先,任何JINI服务都需要一个服务接口,表示这个服务能做什么.我们这 ...

  9. ABAP 从CDS VIEW 发布OData Service示例

    1. 创建可供发布Odata Service 的 CDS VIEW 新建一个简单点CDS VIEW,并添加必要Annotation参数:@OData.publish: true 其他Annotatio ...

最新文章

  1. 广告片断大收集+穿帮镜头
  2. 从零开始学python数据分析-从零开始学Python数据分析与挖掘 PDF 下载
  3. 解决sublime text无法安装插件问题
  4. macos server 恢复安装_Go语言:Docker安装及运行consul节点
  5. Linux 进程信号:信号的概念、生命周期、产生流程、阻塞
  6. 【网络安全】关于ARP攻击的原理以及在Kali Linux环境下的实现
  7. 用JSLint精炼提升JavaScript代码
  8. jmp连mysql_令人迷惑的ATT的jmp:直接跳转和间接跳转 [转]
  9. 有前途的程序员的14个习惯,你有几个?
  10. 最全深度学习资源列表!
  11. 户外lisp导向牌如何安装_安装案例|户外标识牌常用安装方法
  12. 设置时间同步(ntp)详细步骤
  13. 最清楚的01背包问题讲解
  14. 2021/4/23爬虫第五次课(爬虫网络请求模块下下)
  15. 构建请求header fake_useragent安装以及解决方法
  16. PYTHON2.day14
  17. Android11 GPS 流程代码走读
  18. flutter 初视回味
  19. 团队如何限制合适的在制品(WIP)数量
  20. XML 链接语言(XLink) 版本 1.0

热门文章

  1. 在线协作编辑OT算法简介
  2. 软件测试职业发展-王学丹
  3. csv批量读取测试数据
  4. 为什么这么多应届生要进入互联网行业?
  5. win7误删计算机,Win7系统下文件数据被误删了怎么办
  6. 显示菜单栏_mac菜单栏不显示了,如何设置?
  7. 入门mysql执行计划
  8. php psd图层重命名,ps批量修改图层名字的脚本(附批量替换方法)
  9. 2005年计算机课程,2005年暑期非计算机专业计算机基础课程教学研讨会成功举办...
  10. mysql myisam表加索引_MyISAM和InnoDB的索引实现