Eclipse+Maven+Spring+CXF 构建webservice 服务
一、 软件准备
- Eclipse 4.2.1
- Maven 2.2.1
- Spring 3.2.6
- CXF 3.0.2
二、 步骤
首先,在Eclipse中用maven构建一个quickstart版本的maven项目,并且在pom文件中添加4个依赖,分别是:cxf-rt- frontend-jaxws、cxf-rt-databinding-aegis、cxf-rt-transports-http、cxf-rt- transports-http-jetty,在本次测试时用的版本都是2.5.2,以下为本次项目的pom.xml文件中的cxf依赖部分:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-databinding-aegis</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>2.5.2</version>
</dependency>
1. 新建web工程,利用maven管理,如下:
工程名为test,完成以后,项目结构如下图:
src/main/java 准备放 java 程序;
src/main/resources准备放各类资源文件。
2. 添加代码
1) 定义服务接口
package com.test;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
public String sayHello();
}
因为只是一个webservice的实验程序,所以非常简单,只有一个服务方法: sayHello(),利用 @WebService注解来声明这是一个webservice的接口。
2) 实现服务类
package com.test; import javax.jws.WebService; @WebService
public class HelloWorldImpl implements HelloWorld{ public String sayHello(){
return "Hello world!";
}
}
完成java代码添加后的项目结构如下:
3. 添加Spring-CXF 配置
在项目 src/main/webapp/WEB-INF 目录下新建XML定义:cxf-servlet.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!-- START SNIPPET: beans -->
<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.test.HelloWorldImpl" address="/HelloWorld"/>
</beans>
<!-- END SNIPPET: beans -->
该定义文件利用spring和CXF的功能,发布一个ID为helloWorld,实现类为com.test.HelloWorldImpl,发布 相对路径为 /HelloWorld(对应绝对目录为: http://host:port/{WebAPPName}/HelloWorld)的 webservice。
因为我们需要用到CXF来做webservice,右键点击项目中的POM.XML,添加apache-cxf依赖,如下图:
4. Web应用配置
修改 src/main/webapp/WEB-INF 目录下的web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>cxf</display-name>
<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>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
该文件实际上是定义了处理webservice的CXF Servlet的映射关系。
完成步骤3和4以后的工程目录如下:
5. 编译打包
利用maven(package -X)编译打包成test.war
(在Eclipse上右击工程名 Run as -> Maven build)
6. 将步骤5生成的test.war部署到tomcat服务器上
7. 访问测试:
在浏览器上输入:http://localhost:8080/test/,出现如下画面就成功了:
点击WSDL链接:
8. 编写webservice client端代码
1) 首先通过 Spring 与 CXF 的配置来定义 Web Service 的客户端 Bean,在 src\main\resources 目录下创建client-beans.xml 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!-- START SNIPPET: beans -->
<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="com.test.HelloWorld"
factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.test.HelloWorld"/>
<property name="address" value="http://localhost:8080/test/HelloWorld"/>
</bean>
</beans>
<!-- END SNIPPET: beans -->
需要注意的是,该配置文件中的 address需要写成发布服务的绝对路径。
2) 编写客户端java代码: HelloWorldClient.java
package com.test; import org.springframework.context.support.ClassPathXmlApplicationContext; public final class HelloWorldClient { private HelloWorldClient() {
} 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.sayHello();
System.out.println("Response: " + response);
System.exit(0);
// END SNIPPET: client
}
}
注意,代码中HelloWorldclient = (HelloWorld)context.getBean("client"); 的client需要与"client-beans.xml"中的 bean id一致才能找到这个服务。
现在的项目结构如下:
3) 连接测试
在eclipse中直接按HelloWorldClient运行 Run as -> Java Application:
输出的Hello world! 即是我们发布的HelloWorld的方法 sayHello()的输出!这说明从服务发布到客户端连接都成功了。
Eclipse+Maven+Spring+CXF 构建webservice 服务的更多相关文章
- idea+maven+spring+cxf创建webservice应用(二)生成客户端程序
idea+maven+spring+cxf创建webservice应用(二)生成客户端程序,以上一篇为基础"idea+maven+spring+cxf创建webservice应用" ...
- SpringBoot | 第三十四章:CXF构建WebService服务
前言 上一章节,讲解了如何使用Spring-WS构建WebService服务.其实,创建WebService的方式有很多的,今天来看看如何使用apache cxf来构建及调用WebService服务. ...
- jfinal集成spring cxf做webservice服务
链接地址:http://zhengshuo.iteye.com/blog/2154047 废话不说,直接上代码 新增cxf的plugin CXFPlugin package com.jfinal.pl ...
- 搭建web项目结合spring+cxf的webservice服务
服务端: 服务端和客户端都需要引入包 antlr-2.7.7.jar aopalliance-1.0.jar asm-3.3.jar commons-collections-3.2.1.jar com ...
- 【转】构建基于CXF的WebService服务
构建基于CXF的WebService服务 Apache CXF = Celtix+ XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.C ...
- Spring Boot 使用 CXF 调用 WebService 服务
上一张我们讲到 Spring Boot 开发 WebService 服务,本章研究基于 CXF 调用 WebService.另外本来想写一篇 xfire 作为 client 端来调用 webservi ...
- Spring Boot 开发 WebService 服务
WebService 虽然现在大部分互联网企业不太提倡使用,但在以第三方接口为主导的市场,对方来什么接口你还得用什么接口,不可能把接口重写了.例如大部分传统的大型企业都在用 WebService,并且 ...
- CXF发布webService服务以及客户端调用
这篇随笔内容是CXF发布webService服务以及客户端调用的方法 CXF是什么? 开发工作之前需要下载CXF和安装 下载地址:http://cxf.apache.org 安装过程: <1&g ...
- cxf构建webservice的两种方式
一.简介 对于基于soap传输协议的webservice有两种开发模式,代码优先和契约优先的模式.代码优先的模式是通过编写服务器端的代码,使用代码生成wsdl:契约优先模式首先编写wsdl,再通过ws ...
随机推荐
- Flume简介与使用(一)——Flume安装与配置
Flume简介与使用(一)——Flume安装与配置 Flume简介 Flume是一个分布式的.可靠的.实用的服务——从不同的数据源高效的采集.整合.移动海量数据. 分布式:可以多台机器同时运行采集数据 ...
- FPGA异步时钟设计中的同步策略
1 引言 基于FPGA的数字系统设计中大都推荐采用同步时序的设计,也就是单时钟系统.但是实际的工程中,纯粹单时钟系统设计的情况很少,特别是设计模块与外围芯片的通信中,跨时钟域的情况经常不可避免. ...
- uploadify上传控件使用
uploadify是JQuery的一个上传插件,实现的效果非常不错,并且带进度显示,我将给大家演示如何使用uploadify进行图片上传, 1.点我下载http://www.uploadify.com ...
- Android中使用ViewPager实现广告条
我们在使用电商或视频的手机客户端时,通常会看到广告条的效果.在网上搜索时才知道使用的是ViewPager,于是自己也做了一个Demo. 以下是效果图: 图中包括背景图片.文字描述以及白点. 其中Vie ...
- Visual C++ 打印编程技术-内存设备环境
1.内存设备环境 内存设备环境是一个没有设备与它联系的环境.一般利用与某个标准设备环境兼容的内存设备环境把一个位图复制到屏幕上去.为此可以先创建一个与某个标准设备环境兼容的内存设备环境,然后把所要显示 ...
- C# Flash 图片上传案例(结合网上腾讯头像上传Flash插件)
之前遇到过很多次要上传类似头像图片这种功能需求,这次是要求弄一个flash插件上传图片 感谢主,一个偶然机会在网上找到了一个很好的腾讯头像修改的flash插件:插件下载 这个功能采用Ajax访问支持, ...
- 关于GC进行垃圾回收的时机
前言 今天查看一个同事的代码,发现代码中多处地方使用了GC.Collect()方法,我问他为什么这么做,他说感觉程序中定义了好多变量,怕GC回收不及时,用GC.Collect()可以手动掌控GC进行垃 ...
- visual studio vs2010 vs2013 显示详细调试信息方法;vs debug 出错怎么办,你需要的不是答案,是方法。
显示详细的输出信息: 选项--项目和解决方案--生成并运行--MSBuild项目生成输出详细信息: 这样在输出目录就会显示详细的错误信息,可以自己分析了.
- jQuery如何检查某个元素在网页上是否存在
$("ID")获取的永远是对象,即使网页上没有此元素.因此当要用jQuery检查某个元素在网页上是否存在时,不能使用以下代码: if($("#ID")){ // ...
- jQuery选择器(一)
1.#id根据给定的ID匹配一个元素. <div id="notMe"><p>id="notMe"</p></div& ...