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"));
}
}
分享到:
相关推荐
OracleEBS-WebService开发笔记.pdf
SSH CXF webservice 开发笔记demo(包含步骤文档及所需war包)
使用jaxws开发webservice。 Webservice三要素 Wsdl(webservice使用说明书)重点掌握 Soap(jaxws开发webservice的传输协议)重点掌握 UDDI(了解) Webservice的使用场景分析(掌握) 学会jaxws基本开发方法...
Java+WebService利用(cxf)开发笔记,里面有非常好的学习实例,是入门学生的非常好的资料
什么是 Web 服务? 2, 什么是SOAP?3, 什么是Axis? 4, Axis相比Soap v2的优点5, 什么是WSDL? 6, 什么是WSDD?... 8, AXIS的几种服务类型二, 开发,部署Web服务: 三, 在IDE下开发Web服务:
WebService笔记,axis2开发webservice过程讲解,很详细。认真学习肯定对webservice有个很好的认识
webservice开发技术的代码和文档笔记,希望可以帮助到大家
学习完本次公开课,你将深入理解Web Service,快速上手企业开发。 主要讲解内容如下: Web Service是什么? Web service的优势和前景 Web Service和Web服务器的区别? 在什么情况下你应该使用Web service? Web ...
资料包括: 1:《Oracle E-Business Suite:ERP DBA实践指南》 2:EBSr12学习笔记 3: Ebs安装与维护 AIX64 4: ebs打补丁步骤 5:ebs财务客户化报表教程 ...8:ebs webservice开发笔记 9:R12安装手册
2.4.1.1 开发步骤 2.4.1.2 运行wsimport 2.4.1.3 修改生成的Java代码 2.4.1.4 调用Web Service 2.5 SOAP headers 2.5.1 客户端添加SOAP headers 2.5.2 访问SOAP headers 2.5.2.1 服务器端 ...
【精品文档】基于cxf webservice传递List及bean.pdf CXF学习笔记.doc 使用Apache CXF开发Web Service.pdf 如何使用myeclipse开发 webservice
资源名称:SOAP UI 5.5 WebService API 调用工具 内容概要:软件安装压缩包 适用人群:接口(API)开发人员 使用场景:用于 WebService API 调用
Laravel减轻了许多Web项目中使用的常见任务,从而减轻了开发过程中的痛苦,例如: 。 。 用于和存储的多个后端。 富有表现力,直观的 。 数据库不可知。 。 。 Laravel易于访问,功能强大,并提供大型,强大的应用...
06 / 怎么写WebService并发布接口 07 / 怎么在后端拦截接口并修改 ecology-泛微OA 建模引擎文档 建模引擎文档 流程引擎文档 流程引擎文档 系统的接口文档 系统的接口文档 资源很全面,下载不会吃亏,Ecology泛微的...
Apache CXF WebService开发 Packet Apache CXF WebService开发一书源码 先决条件 JDK 1.7.0u71或更高 Maven 3.2.3或更高版本 Apache CXF 2.4.10 读书笔记
项目收集的axis的相关资料~~ Axis.pdf AXIS(Java+WebSerivce)全攻略.mht ...WebService之axis的复杂对象传输方案.txt 使用Apache Axis部署 Web服务时的常见问题及其解决方法.txt 使用Axis开发Web Service程序.txt
6.4:WebService 189 6.5:集群分布式应用(以JBOSS为例) 190 6.6:JNLP原理及应用: 190 6.7:Log4原理及应用: 191 6.8:JFreeChat原理及应用: 191 6.9:几种常用协议 192 7.0:SOA原理 200 8:搜索引擎专题 205 9:CMS...
2.4.1.1 开发步骤 2.4.1.2 运行wsimport 2.4.1.3 修改生成的Java代码 2.4.1.4 调用Web Service 2.5 SOAP headers 2.5.1 客户端添加SOAP headers 2.5.2 访问SOAP headers 2.5.2.1 服务器端 2.5.2.2 ...
11.用.net做B/S结构的系统,您是用几层结构来开发,每一层之间的关系以及为什么要这样分层? 答:一般为3层 数据访问层,业务层,表示层。 数据访问层对数据库进行增删查改。 业务层一般分为二层,业务表观层...