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

现在的项目中需要用到SOA概念的地方越来越多,最近我接手的一个项目中就提出了这样的业务要求,需要在.net开发的客户端系统中访问java开发的web系统,这样的业务需求自然需要通过WebService进行信息数据的操作。下面就将我们在开发中摸索的一点经验教训总结以下,以供大家参考.

我们项目的整个架构使用的比较流行的WSH MVC组合,即webwork2 + Spring + Hibernate;
1.首先集成Apacha CXF WebService 到 Spring 框架中;
apache cxf 下载地址:http://people.apache.org/dist/incubator/cxf/2.0.4-incubator/apache-cxf-2.0.4-incubator.zip
在spring context配置文件中引入以下cxf配置

	<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" />

在web.xml中添加过滤器:

	<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

2.开发服务端WebService接口:

/**
* WebService接口定义类.
*
* 使用@WebService将接口中的所有方法输出为Web Service.
* 可用annotation对设置方法、参数和返回值在WSDL中的定义.
*/
@WebService
public interface WebServiceSample {

/**
* 一个简单的方法,返回一个字符串
* @param hello
* @return
*/
String say(String hello);

/**
* 稍微复杂一些的方法,传递一个对象给服务端处理
* @param user
* @return
*/
String sayUserName(
@WebParam(name = "user")
UserDTO user);

/**
* 最复杂的方法,返回一个List封装的对象集合
* @return
*/
public
@WebResult(partName="o")
ListObject findUsers();

}

由简单到复杂定义了三个接口,模拟业务需求;

3.实现接口

/**
* WebService实现类.
*
* 使用@WebService指向Interface定义类即可.
*/
@WebService(endpointInterface = "cn.org.coral.biz.examples.webservice.WebServiceSample")
public class WebServiceSampleImpl implements WebServiceSample {

public String sayUserName(UserDTO user) {
return "hello "+user.getName();
}

public String say(String hello) {
return "hello "+hello;
}

public ListObject findUsers() {
ArrayList<Object> list = new ArrayList<Object>();

list.add(instancUser(1,"lib"));
list.add(instancUser(2,"mld"));
list.add(instancUser(3,"lq"));
list.add(instancUser(4,"gj"));
ListObject o = new ListObject();
o.setList(list);
return o;
}

private UserDTO instancUser(Integer id,String name){
UserDTO user = new UserDTO();
user.setId(id);
user.setName(name);
return user;
}
}

4.依赖的两个类:用户对象与List对象

/**
* Web Service传输User信息的DTO.
*
* 分离entity类与web service接口间的耦合,隔绝entity类的修改对接口的影响.
* 使用JAXB 2.0的annotation标注JAVA-XML映射,尽量使用默认约定.
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "User")
public class UserDTO {

protected Integer id;

protected String name;

public Integer getId() {
return id;
}

public void setId(Integer value) {
id = value;
}

public String getName() {
return name;
}

public void setName(String value) {
name = value;
}
}

关于List对象,参照了有关JWS的一个问题中的描述:DK6.0 自带的WebService中 WebMethod的参数好像不能是ArrayList 或者其他List
传递List需要将List 包装在其他对象内部才行 (个人理解 如有不对请指出) ,我在实践中也遇到了此类问题.通过以下封装的对象即可以传递List对象.

/**
* <p>Java class for listObject complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="listObject">
*   <complexContent>
*     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
*       <sequence>
*         <element name="list" type="{http://www.w3.org/2001/XMLSchema}anyType" maxOccurs="unbounded" minOccurs="0"/>
*       </sequence>
*     </restriction>
*   </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "listObject", propOrder = { "list" })
public class ListObject {

@XmlElement(nillable = true)
protected List<Object> list;

/**
* Gets the value of the list property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the list property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
*    getList().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link Object }
*
*
*/
public List<Object> getList() {
if (list == null) {
list = new ArrayList<Object>();
}
return this.list;
}

public void setList(ArrayList<Object> list) {
this.list = list;
}

}

5.WebService 服务端 spring 配置文件 ws-context.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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName" default-lazy-init="true">

<jaxws:endpoint id="webServiceSample"
address="/WebServiceSample" implementor="cn.org.coral.biz.examples.webservice.WebServiceSampleImpl"/>

</beans>

WebService 客户端 spring 配置文件 wsclient-context.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://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName" default-lazy-init="true">

<!-- ws client -->
<bean id="identityValidateServiceClient" class="cn.org.coral.admin.service.IdentityValidateService"
factory-bean="identityValidateServiceClientFactory" factory-method="create" />

<bean id="identityValidateServiceClientFactory"
class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass"
value="cn.org.coral.admin.service.IdentityValidateService" />
<property name="address"
value="http://88.148.29.54:8080/coral/services/IdentityValidateService"/>
</bean>

</beans>

6.发布到tomcat服务器以后通过以下地址即可查看自定义的webservice接口生成的wsdl:
http://88.148.29.54:8080/aio/services/WebServiceSample?wsdl

7.调用WebService接口的Junit单元测试程序

package test.coral.sample;

import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

import cn.org.coral.biz.examples.webservice.WebServiceSample;
import cn.org.coral.biz.examples.webservice.dto.UserDTO;

public class TestWebServiceSample extends
AbstractDependencyInjectionSpringContextTests {
WebServiceSample webServiceSampleClient;

public void setWebServiceSampleClient(WebServiceSample webServiceSampleClient) {
this.webServiceSampleClient = webServiceSampleClient;
}

@Override
protected String[] getConfigLocations() {
setAutowireMode(AUTOWIRE_BY_NAME);
//spring 客户端配置文件保存位置
return new String[] { "classpath:/cn/org/coral/biz/examples/webservice/wsclient-context.xml" };
}

public void testWSClinet(){
Assert.hasText(webServiceSampleClient.say(" world"));
}
}

WebService开发笔记 1 -- 利用cxf开发WebService竟然如此简单,布布扣,bubuko.com

时间: 2024-08-05 07:08:15

WebService开发笔记 1 -- 利用cxf开发WebService竟然如此简单的相关文章

[转] WebService开发笔记 1 -- 利用cxf开发WebService竟然如此简单

以下文章来自   http://www.blogjava.net/jacally/articles/186655.html 现在的项目中需要用到SOA概念的地方越来越多,最近我接手的一个项目中就提出了这样的业务要求,需要在.net开发的客户端系统中访问java开发的web系统,这样的业务需求自然需要通过WebService进行信息数据的操作.下面就将我们在开发中摸索的一点经验教训总结以下,以供大家参考. 我们项目的整个架构使用的比较流行的WSH MVC组合,即webwork2 + Spring

利用cxf开发WebService

利用cxf开发WebService 1.什么是CXF Apache CXF =Celtix + Xfire 支持多种协议: ?    SOAP1.1,1,2 ?    XML/HTTP ?    CORBA(Common ObjectRequest Broker Architecture公共对象请求代理体系结构,早期语言使用的WS.C,c++,C#) ?    并可以与Spring进行快速无缝的整合 ?    灵活的部署:可以运行在Tomcat,Jboss,Jetty(内置),IBMWS,Bea

利用CXF开发WebService的小案例

http://www.blogjava.net/ashutc/archive/2009/11/24/303521.html 开发工具:MyEclipse 6.0 开发环境: 1.     jdk1.5 2.     CXF框架,版本apache-cxf-2.2.3.zip,到http://cxf.apache.org/download.html下载 注:如使用jdk1.6进行开发,需下载jaxb-api.jar和jaxws-api.jar,然后在本机安装JDK的地方,在jdk1.6.0的jre文

Hololens开发笔记之使用Unity开发一个简单的应用

一.Hololens概述 Hololens有以下特性 1.空间映射借助微软特殊定制的全息处理单元(HPU),HoloLens 实现了对周边环境的快速扫描和空间匹配.这保证了 HoloLens能够准确地在真实世界表面放置或展现全息图形内容,确保了核心的AR体验. 2.场景匹配HoloLens 设备能存储并识别环境信息,恢复和保持不同场景中的全息图像对象.当你离开当前房间再回来时,会发现原有放置的全息图像均会在正确的位置出现. 3.自然交互HoloLens 主要交互方式为凝视(Gaze).语音(Vo

(SenchaTouch+PhoneGap)开发笔记(2)开发环境搭建二

一.Java环境和Android SDK  1.安装JDK和JRE JRE会在JDK安装完成后自动出现安装界面. 安装完成后,设置环境变量 JAVA_HOME    D:\Program Files\Java\jdk1.7.0_45(不同版本的JDK路径可能不一样) CLASSPATH    .;%JAVA_HOME%\lib(注意开头的.) PATH        %JAVA_HOME%\bin 2.安装Android SDK 下载好的Android SDK是个压缩包,名字类似adt-bund

SenchaTouch开发笔记(1)-开发环境搭建

1.下载senchaTouch 2.下载senchaCMD 3.安装ruby(for windows ) 4.senchaCmd创建项目: 打开senchacmd,cd到senchaTouch的目录如:E:\touch-2.3,执行创建项目命令sencha generate app MyApp D:\MyApp,将会创建一个名为MyApp的项目,路径在D:\MyApp 5.写好你的代码后,使用sencha app build package 命令将写好的app发布,package命令可以将所有的

cxf开发基于web的webservice项目(转载)

其实开发服务端, 大体分为2种方式:一: 采用jdk给我们提供的jas-ws中的服务类来发布服务二: 采用第三方框架来开发webservice.那么为什么我们要选择第三方框架来发布一个webservice服务呢?首先, 我们开发的项目大部分都是javase项目, jdk不能用于javaee项目的开发. 并且jdk目前仅仅支持soap1.1协议. 不支持soap1.2协议 而为了客户端调用时能使用1.1协议, 也能使用1.2协议.通常我们发布的服务都是1.2协议的.下面, 就说下cxf开发服务端,

使用cxf开发webservice接口

项目中经常用到开发webservice接口,及调用webService接口.这里讲解如何使用cxf开发webService接口. 一.webservice介绍及理解 webservice是一种跨平台,跨语言的规范,用于不同平台,不同语言开发的应用之间的交互.        比如,平台平台淘宝.京东想获取其他快递公司数据接口,需快递公司开放数据接口.       那么 webservice就是出于以上类似需求而定义出来的规范:无需关心对方什么平台上开发以及使用何种语言开发.       只关心调用

【转】Android开发笔记(序)写在前面的目录

原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经验教训,与网友互相切磋,从而去芜存菁进一步提升自己的水平.因此博主就想,入门的东西咱就不写了,人不能老停留在入门上:其次是想拾缺补漏,写写虽然小众却又用得着的东西:另外就是想以实用为主,不求大而全,但求小而精:还有就是有的知识点是java的,只是Android开发也会经常遇上,所以蛮记下来.个人的经