1. WebService基本概念
WSDL:
http://www.w3cschool.cn/index-20.html
SOAP:
http://www.w3school.com.cn/soap/index.asp
2. 引入CXF依賴庫
下載:http://cxf.apache.org/,解壓至目錄
加上依賴的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
參考:http://cxf.apache.org/docs/writing-a-service-with-spring.html
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>
啓動後,則可以訪問路徑:http://localhost:8080/joffice21/ws/regService?wsdl
效果如下:
執行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());
}
}
}
}