Joffice2中WebService的使用(CXF)

1. WebService基本概念

WSDL:
[url]http://www.w3cschool.cn/index-20.html[/url]
SOAP:
[url]http://www.w3school.com.cn/soap/index.asp[/url]

2. 引入CXF依赖库

下载:[url]http://cxf.apache.org/[/url],解压至目录
加上依赖的jar库,如:
   commons-logging-1.1.jar  
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.0.jar
jaxb-impl-2.0.5.jar
jaxws-api-2.0.jar
neethi-2.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.1.jar
XmlSchema-1.2.jar
xml-resolver-1.2.jar


参考:[url]http://cxf.apache.org/docs/writing-a-service-with-spring.html[/url]

3. 发布J.Office的Service类为Service



编写RegService接口及实现类,如下所示:(注意@WebService)

3.1 RegService接口
package com.htsoft.oa.service.cxf;  

import java.util.List;

import javax.jws.WebService;

import com.htsoft.oa.model.admin.Regulation;

@WebService(targetNamespace="http://www.jee-soft.cn")
public interface RegService {
public List<Regulation> getAll();
}


3. 2 RegServiceImpl

package com.htsoft.oa.service.cxf.impl;  

import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebService;

import com.htsoft.oa.model.admin.Regulation;
import com.htsoft.oa.service.admin.RegulationService;
import com.htsoft.oa.service.cxf.RegService;

@WebService
public class RegServiceImpl implements RegService{
@Resource
RegulationService regulationService;

@Override
public List<Regulation> getAll() {
return regulationService.getAll();
}
}


4. 在J.Office中加上对外的发布Service

在resources/conf下加一app-cxf.xml,内容如下:
<?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: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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<bean id="regService" class="com.htsoft.oa.service.cxf.impl.RegServiceImpl"/>

<jaxws:endpoint id="regServiceWs" implementor="#regService"
address="/regService" />

</beans>

并且在app-context.xml中引入以上文件

<import resource="app-cxf.xml"/>

在web.xml中加入
<!-- cxf 服务器-->  
<servlet>
<servlet-name>CXFServlet</servlet-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>/service/*</url-pattern>
</servlet-mapping>


启动后,则可以访问路径:[url]http://localhost:8080/joffice21/ws/regService?wsdl[/url]
效果如下:

[img]http://dl.iteye.com/upload/attachment/0071/8539/1def2e0b-789c-3264-b493-e188df6add71.jpg[/img]
执行ant任务,把joffice中的类打包成ht_cxf_client.jar放置客户端环境:
<target name="jarservice-model">  
<jar destfile="build/ht_cxf_client.jar">
<fileset dir="web/WEB-INF/classes">
<include name="com/htsoft/core/**"/>
<include name="com/htsoft/oa/service/**"/>
<include name="com/htsoft/oa/model/**"/>
<exclude name="com/htsoft/oa/model/**/*.hbm.xml"/>
</fileset>
</jar>
</target>

5. 编写测试客户端
package com.cxf;  

import java.util.List;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.htsoft.oa.model.admin.Regulation;
import com.htsoft.oa.service.cxf.RegService;

public class ClientMain {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JaxWsProxyFactoryBean factoryBean=new JaxWsProxyFactoryBean();
factoryBean.setAddress("http://localhost:8080/joffice21/ws/regService");
factoryBean.setServiceClass(RegService.class);

RegService regService=(RegService)factoryBean.create();

List<Regulation> list=regService.getAll();

if(list!=null){
for(Regulation reg:list){
System.out.println(" reg:" + reg.getSubject());
}
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值