标签:apache cxf webservic plsql developer spring mvc mybatis powerdesigner
我们要为出口报运做一个WebService,来提供跨系统的信息查询功能。<!-- 整合Apache CXF WebService --> <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>
我们找到出口报运的Service实现类ExportService,加入@WebService注解:
package cn.hpu.jk.service.impl; import java.io.Serializable; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import javax.annotation.Resource; import javax.jws.WebService; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import cn.hpu.jk.Util.UtilFuns; import cn.hpu.jk.dao.ContractDao; import cn.hpu.jk.dao.ExportDao; import cn.hpu.jk.dao.ExportProductDao; import cn.hpu.jk.dao.ExtEproductDao; import cn.hpu.jk.domain.Contract; import cn.hpu.jk.domain.Export; import cn.hpu.jk.domain.ExportProduct; import cn.hpu.jk.domain.ExtEproduct; import cn.hpu.jk.pagination.Page; import cn.hpu.jk.service.ExportService; import cn.hpu.jk.vo.ContractProductVO; import cn.hpu.jk.vo.ContractVO; import cn.hpu.jk.vo.ExtCproductVO; @Service @WebService public class ExportServiceImpl implements ExportService{ @Resource ExportDao exportDao; @Resource ExportProductDao exportProductDao; @Resource ExtEproductDao extEproductDao; @Resource ContractDao contractDao; @Override public List<Export> find(Map paraMap) { // TODO Auto-generated method stub return exportDao.find(paraMap); } @Override public List<Export> findPage(Page page) { // TODO Auto-generated method stub return exportDao.findPage(page); } @Override public Export get(Serializable id) { // TODO Auto-generated method stub return exportDao.get(id); } @Override public void insert(String [] contractIds) { //步骤 //1.根据合同id获得合同对象,获取合同号 //2.将合同下的货物信息搬到报运下的货物表中 //2.将合同下的附件信息搬到报运下的附件表中 //拼接合同号,报运号 String contractNos=""; for (int i = 0; i < contractIds.length; i++) { ContractVO contract=contractDao.view(contractIds[i]); contractNos+=contract.getContractNo()+" ";//以空格作为分割符 } contractNos=UtilFuns.delLastChar(contractNos);//删除最后一个空格 Export export=new Export(); export.setId(UUID.randomUUID().toString()); //将String数组拼成“x,y,z”形式的字符串 export.setContractIds(UtilFuns.joinStr(contractIds,","));//工具类,拼接字符串 export.setCustomerContract(contractNos); export.setState(0);//默认是草稿状态 exportDao.insert(export); //处理货物以及附件信息 for (int i = 0; i < contractIds.length; i++) { ContractVO contract=contractDao.view(contractIds[i]); for(ContractProductVO cp : contract.getContractProducts()){ //处理货物信息 ExportProduct ep=new ExportProduct(); ep.setId(UUID.randomUUID().toString()); ep.setExportId(export.getId()); //数据搬家,将合同下的对应的货物信息写入到报运下的货物信息中 ep.setFactoryId(cp.getFactory().getId()); ep.setFactoryName(cp.getFactoryName()); ep.setProductNo(cp.getProductNo()); ep.setPackingUnit(cp.getPackingUnit()); ep.setCnumber(cp.getCnumber()); ep.setBoxNum(Integer.parseInt(cp.getBoxNum())); ep.setPrice(cp.getPrice()); exportProductDao.insert(ep); //处理附件信息 for (ExtCproductVO extcp : cp.getExtCproducts()) { ExtEproduct extep=new ExtEproduct(); //BeanUtils是Spring提供的工具类,实现数据的拷贝 BeanUtils.copyProperties(extcp, extep); //拷贝的数据并不能完全使用,我们要修改其中的部分数据 extep.setId(UUID.randomUUID().toString()); extep.setExportProductId(ep.getId());//绑定外键 extep.setFactoryId(extcp.getFactory().getId()); extep.setFactoryName(extcp.getFactory().getFactoryName()); extEproductDao.insert(extep); } } } } @Override public void update(Export export,String [] mr_id, Integer [] mr_orderNo,Integer [] mr_cnumber, Double [] mr_grossWeight,Double [] mr_netWeight, Double [] mr_sizeLength,Double [] mr_sizeWidth, Double [] mr_sizeHeight,Double [] mr_exPrice, Double [] mr_tax,Integer [] mr_changed) { exportDao.update(export); //批量修改货物信息 for (int i = 0; i < mr_id.length; i++) { System.out.println(mr_changed[i]+"#####################"); if(mr_changed[i]!=null && mr_changed[i]==1){ //修改标识,只有用户修改的才进行更新 ExportProduct ep=exportProductDao.get(mr_id[i]); ep.setOrderNo(mr_orderNo[i].toString()); ep.setCnumber(mr_cnumber[i]); ep.setGrossWeight(mr_grossWeight[i]); ep.setNetWeight(mr_netWeight[i]); ep.setSizeLength(mr_sizeLength[i]); ep.setSizeWidth(mr_sizeWidth[i]); ep.setSizeHeight(mr_sizeHeight[i]); ep.setExPrice(mr_exPrice[i]); ep.setTax(mr_tax[i]); exportProductDao.update(ep); } } } @Override public void delete(Serializable[] ids) { exportDao.delete(ids); } @Override public void deleteById(Serializable id) { exportDao.deleteById(id); } @Override public void submit(Serializable[] ids) { Map map=new HashMap(); map.put("state",1);//1启用 map.put("ids", ids); exportDao.updateState(map); } @Override public void cancel(Serializable[] ids) { Map map=new HashMap(); map.put("state",0);//0启用 map.put("ids", ids); exportDao.updateState(map); } @Override public List<Contract> getContractList() { Map paraMap=new HashMap(); paraMap.put("state", 1);//1 已上报 return contractDao.find(paraMap); } //拼接javascript方法串 //addTRRecord(objId, id, productNo, cnumber, grossWeight, netWeight, sizeLength, sizeWidth, sizeHeight, exPrice, tax) @Override public String getMrecordData(String exportId){ Map paraMap=new HashMap(); paraMap.put("exportId", exportId); List<ExportProduct> oList=exportProductDao.find(paraMap); StringBuffer sBuf=new StringBuffer(); for (ExportProduct ep:oList) { sBuf.append("addTRRecord(\"mRecordTable\", \"") .append(ep.getId()).append("\", \"").append(ep.getProductNo()) .append("\", \"").append(ep.getCnumber()).append("\", \"").append(UtilFuns.convertNull(ep.getGrossWeight())) .append("\", \"").append(UtilFuns.convertNull(ep.getNetWeight())).append("\", \"").append(UtilFuns.convertNull(ep.getSizeLength())) .append("\", \"").append(UtilFuns.convertNull(ep.getSizeWidth())).append("\", \"").append(UtilFuns.convertNull(ep.getSizeHeight())) .append("\", \"").append(UtilFuns.convertNull(ep.getExPrice())).append("\", \"").append(UtilFuns.convertNull(ep.getTax())).append("\");"); } return sBuf.toString(); } }
public class Export implements Serializable{ //代码省略 }
@WebMethod(exclude=true) public void delete(String[] ids) { exportDao.delete(ids); }
@Service @WebService public class ExportServiceImpl implements ExportService{ @Resource ExportDao exportDao; //利用set方法,在cxf中注入Dao,这样cxf的WebService才可以查询我们系统的数据 @WebMethod(exclude=true) public void setExportDao(ExportDao exportDao) { this.exportDao = exportDao; } //下面代码省略 }
<?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"/> </beans>
<?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"/> <span style="white-space:pre"> </span><!-- 声明WebService Bean,发布WebService --> <bean id="exportService" class="cn.hpu.jk.service.impl.ExportServiceImpl"> <property name="exportDao"> <ref bean="exportDaoImpl"/> </property> </bean> <!-- 发布WebService --> <!-- http://localhost:8080/jk/cxf/ExportServiceImpl --> <jaxws:endpoint implementor="#exportService" address="/ExportServiceImpl"/> </beans>
<!-- 3.配置CXF webserice --> <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>
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.jk.hpu.cn/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="ExportServiceImplService" targetNamespace="http://impl.service.jk.hpu.cn/"> <wsdl:types> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://impl.service.jk.hpu.cn/" elementFormDefault="unqualified" targetNamespace="http://impl.service.jk.hpu.cn/" version="1.0"> <xs:element name="get" type="tns:get"/> <xs:element name="getResponse" type="tns:getResponse"/> <xs:complexType name="get"> <xs:sequence> <xs:element minOccurs="0" name="arg0" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="getResponse"> <xs:sequence> <xs:element minOccurs="0" name="return" type="tns:export"/> </xs:sequence> </xs:complexType> <xs:complexType name="export"> <xs:sequence> <xs:element minOccurs="0" name="consignee" type="xs:string"/> <xs:element minOccurs="0" name="contractIds" type="xs:string"/> <xs:element minOccurs="0" name="createBy" type="xs:string"/> <xs:element minOccurs="0" name="createDept" type="xs:string"/> <xs:element minOccurs="0" name="createTime" type="xs:dateTime"/> <xs:element minOccurs="0" name="customerContract" type="xs:string"/> <xs:element minOccurs="0" name="destinationPort" type="xs:string"/> <xs:element minOccurs="0" name="epnum" type="xs:string"/> <xs:element minOccurs="0" name="extnum" type="xs:string"/> <xs:element minOccurs="0" name="grossWeight" type="xs:double"/> <xs:element minOccurs="0" name="id" type="xs:string"/> <xs:element minOccurs="0" name="inputDate" type="xs:dateTime"/> <xs:element minOccurs="0" name="lcno" type="xs:string"/> <xs:element minOccurs="0" name="marks" type="xs:string"/> <xs:element minOccurs="0" name="measurement" type="xs:string"/> <xs:element minOccurs="0" name="netWeight" type="xs:double"/> <xs:element minOccurs="0" name="priceCondition" type="xs:string"/> <xs:element minOccurs="0" name="remark" type="xs:string"/> <xs:element minOccurs="0" name="shipmentPort" type="xs:string"/> <xs:element minOccurs="0" name="state" type="xs:int"/> <xs:element minOccurs="0" name="transportMode" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema> </wsdl:types> <wsdl:message name="getResponse"> <wsdl:part element="tns:getResponse" name="parameters"></wsdl:part> </wsdl:message> <wsdl:message name="get"> <wsdl:part element="tns:get" name="parameters"></wsdl:part> </wsdl:message> <wsdl:portType name="ExportServiceImpl"> <wsdl:operation name="get"> <wsdl:input message="tns:get" name="get"></wsdl:input> <wsdl:output message="tns:getResponse" name="getResponse"></wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="ExportServiceImplServiceSoapBinding" type="tns:ExportServiceImpl"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="get"> <soap:operation soapAction="" style="document"/> <wsdl:input name="get"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="getResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="ExportServiceImplService"> <wsdl:port binding="tns:ExportServiceImplServiceSoapBinding" name="ExportServiceImplPort"> <soap:address location="http://localhost/jx-Maven-Webapp/cxf/ExportServiceImpl"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
package cn.hpu.jk.controller.ws; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; //在系统中调用cxf提供的WebService @Controller public class WSExportController { @RequestMapping("/ws/export/toedit.action") public String toedit(){ return "/ws/export/ajaxExport.jsp"; } }
<li><a href="${ctx }/ws/export/toedit.action" onclick="linkHighlighted(this)" target="main" id="aa_1">WS出口报运单</a></li>
效果:
编辑/ws/export/ajaxExport.jsp页面之前,我们先了解一下如何soap发送xml和接收xml信息。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://impl.service.jk.hpu.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <q0:get> <arg0>39cd8f20-50f8-4bc1-96d5-de6de3d7c8b4</arg0> </q0:get> </soapenv:Body> </soapenv:Envelope>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:getResponse xmlns:ns2="http://impl.service.jk.hpu.cn/"> <return> <consignee>南京</consignee> <contractIds>392bf3b2-cb0e-4e7a-ba02-c91ce83e9fe1,55743f24-f092-47ac-b149-358785437238</contractIds> <customerContract>3 2</customerContract> <destinationPort>深圳港</destinationPort> <id>39cd8f20-50f8-4bc1-96d5-de6de3d7c8b4</id> <inputDate>2015-10-09T00:00:00+08:00</inputDate> <lcno>T/T</lcno> <marks>11</marks> <priceCondition>2</priceCondition> <remark>22</remark> <shipmentPort>连云港</shipmentPort> <transportMode>1</transportMode> </return> </ns2:getResponse> </soap:Body> </soap:Envelope>
<%@ page language="java" pageEncoding="UTF-8"%> <%@ include file="../../base.jsp"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>出口报运跟踪</title> <script language="javascript" src="${ctx}/js/datepicker/WdatePicker.js"></script> <script type="text/javascript"> /* 开发步骤 1.创建xmlHttpRquest对象 2.open('POST',url,true)链接 3.设置请求头 4.send(xml) 5.回调函数,接收响应xml 6.从返回的XML抓取我们相关的信息 */ var xmlHttpRequest; if(window.XMLHttpRequest){ //针对FireFox、Mozillar、Opera、Safari、IE7、IE8 xmlHttpRequest=new XMLHttpRequest(); //修复类似Mozillar浏览器的bug if(xmlHttpRequest.overrideMimeType){ xmlHttpRequest.overrideMimeType("text/xml"); } }else if(window.ActiveXObject){ //所有的IE中window.ActiveXObject条件都成立 //针对IE6、IE5.5、IE5(现在没人用了,可以把这条if分支删除) //两个可以用于创建XMLHttpRequest对象的控件名称,保存在一个js的数组中 //排在前面的版本最新 var activeName=["MSXML2.XMLHTTP","Microsoft.XMLHTTP"]; for(var i=0;i<activeName.length;i++){ try{ //获取一个控件名进行创建,如果创建成功就终止循环 //如果创建失败,会抛出异常,然后就可以继续循环,继续尝试创建 xmlHttpRequest=new ActiveXObject(activeName[i]); break; }catch(e){ //仍然不能创建,抛出异常后,给出友好提示 } } } //发送soap请求 function sendMsg(){ var url="http://localhost/jx-Maven-Webapp/cxf/ExportServiceImpl";//WebService请求路径 var reuqestBody="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +"xmlns:q0=\"http://impl.service.jk.hpu.cn/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +"<soapenv:Body><q0:get> <arg0>" +"39cd8f20-50f8-4bc1-96d5-de6de3d7c8b4" +"</arg0> </q0:get></soapenv:Body></soapenv:Envelope>"; xmlHttpRequest.open("POST",url,true);//打开链接 xmlHttpRequest.setRequestHeader("Content-Type","text/xml;charset=utf-8");//设置请求头 xmlHttpRequest.send(reuqestBody);//发送soap请求 xmlHttpRequest.onreadystatechange=_back;//在onreadystatechange事件上设置回调函数 } //回调函数 function _back(){ if(xmlHttpRequest.readyState==4){//提交完成后 if(xmlHttpRequest.status==200){ alert(xmlHttpRequest.responseXML); }else{ alert("访问失败!"); } } } </script> </head> <body> <form method="post"> <div id="menubar"> <div id="middleMenubar"> <div id="innerMenubar"> <div id="navMenubar"> <ul> <li id="save"><a href="#" onclick="sendMsg();">查看</a></li> </ul> </div> </div> </div> </div> <div class="textbox" id="centerTextbox"> <div class="textbox-header"> <div class="textbox-inner-header"> <div class="textbox-title"> 出口报运跟踪 </div> </div> </div> <div> <div> <table class="commonTable" cellspacing="1"> <tr> <td class="columnTitle_mustbe">合同或确认书号:</td> <td class="tableContent">${obj.customerContract }</td> <td class="columnTitle_mustbe">制单日期:</td> <td class="tableContent"> <fmt:formatDate value="${obj.inputDate }" pattern="yyyy-MM-dd"/> </td> </tr> <tr> <td class="columnTitle_mustbe">信用证号:</td> <td class="tableContent">${obj.lcno }</td> <td class="columnTitle_mustbe">收货人及地址:</td> <td class="tableContent">${obj.consignee}</td> </tr> <tr> <td class="columnTitle_mustbe">装运港:</td> <td class="tableContent">${obj.shipmentPort}</td> <td class="columnTitle_mustbe">目的港:</td> <td class="tableContent">${obj.destinationPort}</td> </tr> <tr> <td class="columnTitle_mustbe">价格条件:</td> <td class="tableContent"> ${obj.priceCondition} </td> <td class="columnTitle_mustbe">运输方式:</td> <td class="tableContent"> ${obj.transportMode} </td> </tr> <tr> <td class="columnTitle_mustbe">唛头:</td> <td class="tableContent">${obj.marks}</td> <td class="columnTitle_mustbe">备注:</td> <td class="tableContent">${obj.remark}</td> </tr> </table> </div> </div> </form> </body> </html>
我们下一篇总结再来谈如何解析回复的XML信息。
转载请注明出处:http://blog.csdn.net/acmman/article/details/49070705
版权声明:本文为博主原创文章,未经博主允许不得转载。
【springmvc+mybatis项目实战】杰信商贸-34.业务出口报运WebService1
标签:apache cxf webservic plsql developer spring mvc mybatis powerdesigner
原文地址:http://blog.csdn.net/acmman/article/details/49070705