第十天 整合ApacheCXF实现WebService服务
1.CXF apache webservice
简单快捷的开发一个web service程序
a)需求:
杰信系统是一个内部使用的业务系统,
VPN 企业向电信申请在inter网上的内部网。
客户实时了解订单情况,了解订单走到货运流程的哪一步。
原先:客户给销售打电话,销售人员询问公司同事,问船务。船务回馈信息。
给客户系统提供一个webservice服务,让他能实时查询订单的状态,
实现上面的要求,在出口报运中提供这个web service服务。
将出口报运查看发布成WebService服务,在这里客户可以随时查看出口报运单,查看订单的流程。
b)开发步骤:
1)引入apache cxf jar
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.0-milestone2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.0.0-milestone2</version>
</dependency>
<!-- Jetty is needed if you're using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.0.0-milestone2</version>
</dependency>
2)改造出口报运单Service为WebService
WebService公开方法中的参数不能使接口,必须实现类。
类改造为:
@WebService
public class ContractServiceImpl implements ContractService {
将不需要公开的方法设置为
@WebMethod(exclude=true)
实体po对象,必须序列化
public class Export implements Serializable
3)配置cxf-servlet.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-servlet.xml"/>
<!-- 将cxf和spring进行整合 -->
<!-- 声明服务 -->
<bean id="exportService" class="cn.itcast.jk.service.impl.ExportServiceImpl">
<!-- 注入spring容器中dao -->
<property name="exportDao" ref="exportDaoImpl"/>
</bean>
<!-- 发布服务 implementor实现类,address发布访问地址-->
<jaxws:endpoint implementor="#exportService" address="/ExportServiceImpl"/>
</beans>
4)Web.xml配置
<!-- Apache CXF servlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<init-param>
<param-name>config-location</param-name>
<param-value>classpath:cxf-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/cxf/*</url-pattern>
</servlet-mapping>
5)发布服务 wsdl WebService说明书
http://localhost/jk/cxf/ExportServiceImpl?wsdl
6)模拟调用这个webService服务 ajax SOAP xml
<script type="text/javascript">
var xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
function sendMsg(){
/*
1.创建ajax对象 XMLHTTP
2.打开一个链接open("POST",url,true)
3.soap xml request
4.设置requestHeader请求头
5.回调函数,处理返回的xml,将信息展现到页面
6.send(xml)
*/
var findId = document.getElementById("findId").value;
findId = "%"+findId+"%";
if(findId==null){
alert("请输入编号");
return false;
}
var requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:q0=\"http://impl.service.jk.itcast.cn/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
+ "<soapenv:Body><q0:wsview><arg0>"+findId+"</arg0></q0:wsview></soapenv:Body></soapenv:Envelope>"
var url = "${ctx}/cxf/ExportServiceImpl";
xmlHttpRequest.open("POST", url , true);
xmlHttpRequest.setRequestHeader("Content-Type","text/xml;charset=utf-8;");
xmlHttpRequest.onreadystatechange = _back;
xmlHttpRequest.send(requestBody);
}
function _back(){
if(xmlHttpRequest.readyState==4){ //处理完成
if(xmlHttpRequest.status==200){
var retxml = xmlHttpRequest.responseXML; //返回的XML内容
var ret = retxml.getElementsByTagName("return")[0];
if(ret==null){
alert("查询不到记录!");
return false;
}
//将数据插入到对应的td
document.getElementById("customerContract").innerHTML = ret.getElementsByTagName("customerContract")[0].text;
if(ret.getElementsByTagName("inputDate")[0]!=null){
document.getElementById("inputDate").innerHTML = ret.getElementsByTagName("inputDate")[0].text.substring(0,10);
}
if(ret.getElementsByTagName("consignee")[0]!=null){
document.getElementById("consignee").innerHTML = ret.getElementsByTagName("consignee")[0].text;
}
if(ret.getElementsByTagName("lcno")[0]!=null){
document.getElementById("lcno").innerHTML = ret.getElementsByTagName("lcno")[0].text;
}
if(ret.getElementsByTagName("shipmentPort")[0]!=null){
document.getElementById("shipmentPort").innerHTML = ret.getElementsByTagName("shipmentPort")[0].text;
}
if(ret.getElementsByTagName("transportMode")[0]!=null){
document.getElementById("transportMode").innerHTML = ret.getElementsByTagName("transportMode")[0].text;
}
if(ret.getElementsByTagName("priceCondition")[0]!=null){
document.getElementById("priceCondition").innerHTML = ret.getElementsByTagName("priceCondition")[0].text;
}
if(ret.getElementsByTagName("marks")[0]!=null){
document.getElementById("marks").innerHTML = "<pre>"+ret.getElementsByTagName("marks")[0].text+"</pre>";
}
if(ret.getElementsByTagName("remark")[0]!=null){
document.getElementById("remark").innerHTML = "<pre>"+ret.getElementsByTagName("remark")[0].text+"</pre>";
}
}else{
//出错信息
alert("你出错了!")
}
}
}