首页 > 代码库 > erp10-webService

erp10-webService

webService是跨语言的远程调用技术,传递的是xml文件
CXF框架用来解析xml文件

常用的远程调用技术:
1、Socket 套接字  TCP/IP   UDP
2、webservice    跨语言    因为传输的数据是XML·    可以和spring进行整合
3、http调用
4、RML(远程方法调用)   Hessian框架    只能用于java编写的项目



第一节、webService的demo

一、服务端

1、pom.xml:
需要添加spring的依赖(省略)
和CXF的依赖
  1. <dependency>
  2. <groupId>org.apache.cxf</groupId>
  3. <artifactId>cxf-rt-frontend-jaxws</artifactId>
  4. <version>3.0.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.cxf</groupId>
  8. <artifactId>cxf-rt-transports-http</artifactId>
  9. <version>3.0.1</version>
  10. </dependency>

2、加入web.xml  
添加spring的listener contextloaderlister    --contextconfigerlocation    --classpath:applicationContext.xml

3、创建接口和类
  1. package com.itcast.weather;
  2. import javax.jws.WebService;
  3. @WebService
  4. public interface IWeatherService {
  5. public String getWeatherInFoByCityName( String cityName);
  6. }
需要在接口上加@webservice注解,CXF才能使用

  1. package com.itcast.weather;
  2. public class WeatherService implements IWeatherService {
  3. @Override
  4. public String getWeatherInFoByCityName(String cityName) {
  5. if ("北京".equals(cityName)) {
  6. return "轻度雾霾";
  7. }
  8. return "晴天";
  9. }
  10. }
4、在项目中加入配置文件
webservice配置:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
  7. <bean id="weatherService" class="com.itcast.weather.WeatherService" ></bean>
  8. <jaxws:server address="/weather" >
  9. <jaxws:serviceBean>
  10. <ref bean="weatherService" />
  11. </jaxws:serviceBean>
  12. </jaxws:server>
  13. </beans>

CXFServlet配置(web.xml)
  1. <servlet>
  2. <servlet-name>webservice</servlet-name>
  3. <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  4. </servlet>
  5. <servlet-mapping>
  6. <servlet-name>webservice</servlet-name>
  7. <url-pattern>/ws/*</url-pattern>
  8. </servlet-mapping>

此时的访问路径为:
http://localhost:8080/CXFServer/ws/weather?wsdl
技术分享
 




二、客户端

1、和服务端添加同样的依赖
2、运用jdk里面的工具将xml转换成java代码
技术分享
 技术分享
 方法:
进入到项目目录中,在想要生成java文件的地方按住shift右键,打开命令行
技术分享
 输入wsimport -s  .  http://localhost:8080/CXFServer/ws/weather?wsdl
    ,回车
技术分享
 结果:
技术分享
 生成的文件夹目录如下:
技术分享
 其中只有IweatherService这个接口是有用的。其他的都删掉也可以

3、配置客户端xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
  7. <!-- 把jaxws:client当做一个特殊的bean -->
  8. <jaxws:client id="weatherService" serviceClass="com.itcast.weather.IWeatherService"
  9. address="http://localhost:8080/CXFServer/ws/weather?wsdl">
  10. </jaxws:client>
  11. </beans>

4、测试类Test
  1. public class Test {
  2. public static void main(String[] args) {
  3. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext_cxf.xml");
  4. IWeatherService weatherService = (IWeatherService) context.getBean("weatherService");
  5. String info = weatherService.getWeatherInFoByCityName("北京");
  6. System.out.println(info);
  7. }
  8. }
技术分享


 

第二节:手机号码归属地查询

1、配置文件
<jaxws:client id="code" 
                    serviceClass="cn.com.webxml.MobileCodeWSSoap"
                    address="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl"                 >
        
    </jaxws:client>  
2、wsimport导入接口文件
3、创建测试类:
  1. public static void main(String[] args) {
  2. ApplicationContext app = new ClassPathXmlApplicationContext("classpath:applicationContext_cxf.xml");
  3. MobileCodeWSSoap mobileCode = (MobileCodeWSSoap) app.getBean("code");
  4. String codeInfo = mobileCode.getMobileCodeInfo("1850137", null);
  5. System.out.println(codeInfo);
  6. }



第三节、红日物流BOS系统开发

一、数据库设计-----mysql

运单:
技术分享
 state:
0-待发货      1-已发货     2-已结束

运单明细:
技术分享
 




二、代码生成器生成基本代码
技术分享
 技术分享
 



三、bos两个服务开发
1、pol.xml配置:参上
2、applicationContext_cxf.xml配置:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:jaxws="http://cxf.apache.org/jaxws"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">
  8. <bean id="waybillService" class="cn.itcast.bos.ws.WayBillService" ></bean>
  9. <jaxws:server address="waybill" >
  10. <jaxws:serviceBean>
  11. <ref bean="waybillService" />
  12. </jaxws:serviceBean>
  13. </jaxws:server>
  14. </beans>

3、web.xml配置:
  1. <filter>
  2. <filter-name>struts2</filter-name>
  3. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>struts2</filter-name>
  7. <url-pattern>/admin/*</url-pattern>
  8. </filter-mapping>
  9. <servlet>
  10. <servlet-name>webservice</servlet-name>
  11. <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  12. </servlet>
  13. <servlet-mapping>
  14. <servlet-name>webservice</servlet-name>
  15. <url-pattern>/ws/*</url-pattern>
  16. </servlet-mapping>

4、struts.xml配置:(namespace)
  1. <struts>
  2. <package name="default" namespace="/admin" extends="struts-default">
  3. <!-- -->
  4. <action name="waybill_*" class="waybillAction" method="{1}"></action>
  5. <!-- -->
  6. <action name="waybilldetail_*" class="waybilldetailAction" method="{1}"></action>
  7. </package>
  8. </struts>
5、代码:
接口略,@WebService
  1. public class WayBillService implements IWayBillService {
  2. private IWaybilldetailBiz waybilldetailBiz ;
  3. private IWaybillBiz waybillBiz ;
  4. public void setWaybillBiz(IWaybillBiz waybillBiz) {
  5. this.waybillBiz = waybillBiz;
  6. }
  7. public void setWaybilldetailBiz(IWaybilldetailBiz waybilldetailBiz) {
  8. this.waybilldetailBiz = waybilldetailBiz;
  9. }
  10. /**
  11. * 根据运单号获取运单详情
  12. */
  13. public List<Waybilldetail> getWayBilldetails(Long sn) {
  14. Waybilldetail t1 = new Waybilldetail();
  15. t1.setSn(sn);
  16. List<Waybilldetail> list= waybilldetailBiz.getList(t1, null, null);
  17. return list;
  18. }
  19. /**
  20. * 在线下单预约功能
  21. * state:0待发 1在途 2结束
  22. */
  23. public Long online(Long userid, String toaddress, String addressee, String tele, String info) {
  24. Waybill waybill = new Waybill();
  25. waybill.setAddressee(addressee);
  26. waybill.setInfo(info);
  27. waybill.setState("0");
  28. waybill.setTele(tele);
  29. waybill.setToaddress(toaddress);
  30. waybill.setUserid(userid);
  31. waybillBiz.add(waybill);
  32. return waybill.getSn();
  33. }


  34. }




第四节:erp中调用BOS系统

在erp的biz层把bos系统的依赖加进来

一、配置文件

CXF配置:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:jaxws="http://cxf.apache.org/jaxws"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">
  8. <bean id="waybillService" class="cn.itcast.bos.ws.WayBillService" >
  9. <property name="waybillBiz" ref="waybillBiz" ></property>
  10. <property name="waybilldetailBiz" ref="waybilldetailBiz" ></property>
  11. </bean>
  12. <jaxws:server address="waybill" >
  13. <jaxws:serviceBean>
  14. <ref bean="waybillService" />
  15. </jaxws:serviceBean>
  16. </jaxws:server>
  17. </beans>

二、erp项目的biz层调用bos逻辑

action:
  1. public void waybilldetials(){
  2. List<Waybilldetail> list = ordersBiz.waybilldetials(sn);
  3. write(JSON.toJSONString(list));
  4. }
biz:
  1. public List<Waybilldetail> waybilldetials(Long sn) {
  2. List<Waybilldetail> list = wayBillService.getWayBilldetails(sn);
  3. return list;
  4. }
注入省略

三、前端

  1. $("#waybillBtn").bind("click",function(){
  2. if($("#sn").html()==null||$("#sn").html()==""){
  3. $.messager.alert("提示","没有物流信息");
  4. }
  5. $("#waybillWindow").window("open");
  6. $("#waybillGrid").datagrid({
  7. url:‘orders_waybilldetials.action?sn=‘+$("#sn").html(),
  8. columns:[[
  9. {field:‘exedate‘,title:‘日期‘,width:100},
  10. {field:‘exetime‘,title:‘时间‘,width:100},
  11. {field:‘info‘,title:‘信息‘,width:100}
  12. ]],
  13. singleSelect:true
  14. })
  15. })





第五节、erp中调用在线下单预约服务

出库的逻辑里面添加在线下单功能

biz:

  1. private IWayBillService wayBillService ;
  2. public void setWayBillService(IWayBillService wayBillService) {
  3. this.wayBillService = wayBillService;
  4. }
  5. private ISupplierDao supplierDao;
  6. public void setSupplierDao(ISupplierDao supplierDao) {
  7. this.supplierDao = supplierDao;
  8. }
  9. /**
  10. * 出库逻辑
  11. * @param id
  12. * @param empuuid
  13. * @param storeuuid
  14. */
  15. public void doOutstore(Long id,Long empuuid,Long storeuuid){
  16. // 1、修改订单项状态
  17. // private java.util.Date endtime;//结束日期
  18. // private Long ender;//库管员
  19. // private Long storeuuid;//仓库编号
  20. // private String state;//状态 采购订单0未入库 1已入库 销售订单 0未出库 1已出库
  21. Orderdetail orderdetail = get(id);
  22. orderdetail.setEnder(empuuid);
  23. orderdetail.setStoreuuid(storeuuid);
  24. orderdetail.setEndtime(new Date());
  25. orderdetail.setState("1");
  26. // 2、修改库存表
  27. // 2.1 先从数据库中查询是否有此商品的记录:传的条件:仓库ID 商品ID
  28. Storedetail t1= new Storedetail();
  29. t1.setGoodsuuid(orderdetail.getGoodsuuid());
  30. t1.setStoreuuid(orderdetail.getStoreuuid());
  31. List<Storedetail> list = storedetailDao.getList(t1, null, null);
  32. // 如果有记录
  33. if (list!=null&&list.size()>0) {
  34. t1=list.get(0); // if(t1.getNum()>=0){ //如果足够出库 数量递减
  35. t1.setNum(t1.getNum()-orderdetail.getNum());
  36. //
  37. // }else{//如果数据不够出库 抛异常
  38. //
  39. // }
  40. if (t1.getNum()<0) {
  41. throw new ErpException("库存不足");
  42. }
  43. }else{
  44. throw new ErpException("这个仓库没有此商品");
  45. }
  46. // 3、添加一个流水记录
  47. // private Long uuid;//编号
  48. // private Long empuuid;//员工编号
  49. // private java.util.Date opertime;//操作日期
  50. // private Long storeuuid;//仓库编号
  51. // private Long goodsuuid;//商品编号
  52. // private Long num;//数量
  53. // private String type;//类型 1采购订单-入库 2销售订单--出库
  54. Storeoper storeoper = new Storeoper();
  55. storeoper.setEmpuuid(empuuid);
  56. storeoper.setGoodsuuid(orderdetail.getGoodsuuid());
  57. storeoper.setNum(orderdetail.getNum());
  58. storeoper.setOpertime(new Date());
  59. storeoper.setStoreuuid(storeuuid);
  60. storeoper.setType("2");
  61. storeoperDao.add(storeoper);
  62. // 4、修改订单状态
  63. // 前提条件:此订单下的所有订单项都已出库才修改订单状态
  64. // select * from orderdetail where ordersuuid=1 and state=0--有数据 表示有未出库的订单项
  65. Orders orders = orderdetail.getOrders();
  66. Orderdetail detail1 =new Orderdetail();
  67. detail1.setState("0");
  68. detail1.setOrders(orders);
  69. long count = orderdetailDao.getCount(detail1, null,null);
  70. if (count==0) {
  71. // private java.util.Date endtime;//结束日期
  72. // private Long ender;//库管员
  73. orders.setEnder(empuuid);
  74. orders.setEndtime(new Date());
  75. orders.setState("3");
  76. // private String state;//订单状态 采购订单 0为审核 1已审核 2已确认 3已结束 销售订单 0未结束 3已结束
  77. Supplier supplier = supplierDao.get(orders.getSupplieruuid());
  78. List<Orderdetail> orderdetails = orders.getOrderdetails();
  79. String info="";
  80. for (Orderdetail detail : orderdetails) {
  81. info+=detail.getGoodsname()+":"+detail.getNum()+" ";
  82. }
  83. Long sn = wayBillService.online(1l, supplier.getAddress(),supplier.getContact() , supplier.getTele(),info);
  84. orders.setWaybillsn(sn);
  85. }
  86. }

第六节、

webservice远程调用技术   cxf封装了webservice
cxf封装的核心内容是xml
webservice在网络传输的内容是xml    网络协议是soap协议====http协议+xml
验证:


































null


erp10-webService