相关博客:

【WebService】——入门实例

【WebService】——SOAP、WSDL和UDDI

前言:

之前的几篇博客基本上都是使用jdk来实现WebService的调用,没有使用任何框架的东西。这样的操作对WebService客户端、服务端的调用、生成等关系的理解会比较深刻,当然也有很大的弊端,需要手动拷贝生成的代码,进行大量重复性的工作。这时候就需要引入框架了,本篇博客介绍其中一种框架CXF,他可以和spring整合实现WebService。

1、下载CXF

apache-cxf-2.5.0

解压之后可以看到它的目录结构,其中bin中主要是一些bat文件或命令,后面会经常用到的就是wsdl2java。lib中是一系列的jar包。因为cxf支持spring,自带的lib中有一些spring的核心包。

2、环境变量

在Path的值后加“;E:\software\apache-cxf-2.5.0\bin” 注意:加上;分号

测试是否配置成功:

cd E:\software\apache-cxf-2.5.0\bin
e:
wsdl2java

如果出现如下信息,则说明已经可以找到该命令,环境变量配置成功。

3、新建项目(服务端)

引入jar包:

将cxf解压文件的lib目录下的jar包加到项目中。

与前几篇博客相同,我们在服务端给出一个接口IHelloWorld和实现类HelloWorldImpl。

IHelloWorld接口的方法为:

@WebService
public interface IHelloWorld {
public String sayHello(String text);
}

实现类HelloWorldImpl。

@WebService(endpointInterface="cn.com.service.IHelloWorld")
public class HelloWorldImpl implements IHelloWorld {
public String sayHello(String text) {
return "Hello" + text ;
}
}

配置文件

1)applicationContext.xml

在src下新建该文件,其中import标签引入的三个配置文件在cxf的核心jar包下,具体路径:cxf-2.5.0.jar\META-INF\cxf。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id="hello" class="cn.com.service.HelloWorldImpl"/> <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /> </beans>

2)web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<!-- 该listener保证 在web应用启动时加载spring容器 -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!--所有来自/*的请求,都交由CXFServlet处理 -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<!-- <display-name>CXF Servlet</display-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>/*</url-pattern>
</servlet-mapping>
</web-app>

发布服务

将项目部署到tomcat上,启动后在浏览器地址输入http://localhost:8080/webws/HelloWorld?wsdl (webws为项目名称)。

<wsdl:definitions name="HelloWorldImplService" targetNamespace="http://service.com.cn/">
<wsdl:portType name="HelloWorldImpl"></wsdl:portType>
<wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorldImpl">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
</wsdl:binding>
<wsdl:service name="HelloWorldImplService">
<wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort">
<soap:address location="http://localhost:8080/webws/HelloWorld"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

4、生成客户端

新建一个空的客户端项目,进入src目录,使用wsdl2java生成客户端。

Refresh刷新项目,可以看到,客户端项目中生成的相应的文件,效果和使用wsimport命令相同。

测试:

在客户端新建测试类,注意在测试的时候要保证tomcat上部署好服务。

public static void main(String[] args) {
HelloWorldImplService factory=new HelloWorldImplService();
IHelloWorld hw=factory.getHelloWorldImplPort();
System.out.println(hw.sayHello("Sherry"));
}

以上就是cxf整合spring实现WebService调用的例子。小编认为,cxf框架实现WebService的亮点有两个:一是将生成客户端的命令进行了封装,省去了用jdk开发时复杂的命令输入和繁琐的文件拷贝工作。二是整合spring,spring框架的优点就不用多说了,大大提高了开发的效率和可扩展性。

【WebService】——CXF整合Spring的更多相关文章

  1. WebService—CXF整合Spring实现接口发布和调用过程

    一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId> ...

  2. webservice 服务端例子+客户端例子+CXF整合spring服务端测试+生成wsdl文件 +cxf客户端代码自动生成

    首先到CXF官网及spring官网下载相关jar架包,这个不多说.webservice是干嘛用的也不多说. 入门例子 模拟新增一个用户,并返回新增结果,成功还是失败. 大概的目录如上,很简单. Res ...

  3. CXF整合Spring开发WebService

    刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开 ...

  4. 【Java EE 学习 81】【CXF框架】【CXF整合Spring】

    一.CXF简介 CXF是Apache公司下的项目,CXF=Celtix+Xfire:它支持soap1.1.soap1.2,而且能够和spring进行快速无缝整合. 另外jax-ws是Sun公司发布的一 ...

  5. CXF整合Spring之JaxWsProxyFactoryBean调用

    1.见解 1.1 客户端的接口代码还一定要和服务端的接口代码一样,连注解都要一样,不够灵活 1.2 当客户端访问服务器的请求地址时,如果服务端没有对应的地址,就会报错,但是又没有cxf的异常捕获处理 ...

  6. cxf整合spring错误为:cvc-complex-type.2.4.c

    cxf整合spring,报错信息如下: Multiple annotations found at this line:- cvc-complex-type.2.4.c: The matching w ...

  7. cxf整合spring中出现的错误

    Caused by: java.lang.ClassNotFoundException: javax.wsdl.extensions.ElementExtensible at org.apache.c ...

  8. CXF整合Spring发布WebService实例

    一.说明: 上一篇简单介绍了CXF以及如何使用CXF来发布一个简单的WebService服务,并且介绍了客户端的调用. 这一篇介绍如何使用CXF与spring在Web项目中来发布WebService服 ...

  9. CXF整合spring

    近公司需要弄webservics,还说不用框架整合(提倡使用hessian,他们既然说与操作系统有兼容问题,由于人员单薄,不得不屈服,哎),我想了老半天没弄明白他说的不用框架整合spring,尝试过直 ...

随机推荐

  1. flask笔记(一)

    1.第一个flask项目 # 首先你要安装flask这个模块 pip install flask # 安装好了之后,直接新建一个py文件,开始写最简单的flask项目了 from flask impo ...

  2. 微信小程序navigator无法跳转情况

    情况有三种 跳转的页面没有在app.json中注册 跳转的路径不正确 以上两种在命令行(console)中都会提示 跳转的页面在TabBar中,需要将open-type属性是设置为switchTab

  3. CentOS7 安装 Docker,10分钟入门!

    本次安装是在VM虚拟机的CentOS 7环境下,仅为了学习和测试的简单安装,如果在真实生产环境还需要考虑安全策略的其他问题. 1.Linux内核版本需要 3.10.0 以上并且是64位 [root@l ...

  4. 汉化manjaro下的火狐浏览器

    1.下载 汉化包 sudo pacman -S firefox-i18n-zh-cn 2.查看add-ons下的language选项有没有已安装的包 3.在浏览器的地址栏输入 搜索intl.local ...

  5. javascript--select标签的添加删除功能的使用

    在网页开发中,常常遇见这种问题,给定两个框,A和B,和几个图片按钮,A中存在几个操作,点击图片按钮,填加至B中,或者从B中移除等,这种效果如何实现,本文加以总结. 几种效果图如下: 原始图:      ...

  6. django模型的字段查询

    条件运算符 exact: 查判等 list=BookInfo.objects.filter(id__exact=1) 可简写为: list=BookInfo.objects.filter(id=1) ...

  7. Python变量和循环

    1.Python变量 比C语言,Java语言更加简洁,不需要加int等等类型定义,直接变量名 = 值,Python里甚至不需要分号.有些特定的不能当做变量名,变量只能由字母.数字和下划线组成,下划线可 ...

  8. html 弹框 优化 alert

    <!DOCTYPE html> <html> <head> <title>cs</title> </head> <styl ...

  9. C语言实例解析精粹学习笔记——30

    实例30: 用已知字符串s中的字符,生成由其中n个字符组成的所有字符排列.设n小于字符串s的字符个数,其中s中的字符在每个排列中最多出现一次.例如,对于s[]="abc",n=2, ...

  10. 多线程编程以及socket编程_Linux程序设计4chapter15

    看了Linux程序设计4中文版,学习了多线程编程和socket编程.本文的程序参考自Linux程序设计4的第15章. 设计了一个客户端程序,一个服务端程序.使用TCP协议进行数据传输. 客户端进程创建 ...