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的依赖
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.0.1</version>
</dependency>
2、加入web.xml
添加spring的listener contextloaderlister --contextconfigerlocation --classpath:applicationContext.xml
3、创建接口和类
package com.itcast.weather;
import javax.jws.WebService;
@WebService
public interface IWeatherService {
public String getWeatherInFoByCityName( String cityName);
}
需要在接口上加@webservice注解,CXF才能使用
package com.itcast.weather;
public class WeatherService implements IWeatherService {
@Override
public String getWeatherInFoByCityName(String cityName) {
if ("北京".equals(cityName)) {
return "轻度雾霾";
}
return "晴天";
}
}
4、在项目中加入配置文件
webservice配置:
<?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">
<bean id="weatherService" class="com.itcast.weather.WeatherService" ></bean>
<jaxws:server address="/weather" >
<jaxws:serviceBean>
<ref bean="weatherService" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
CXFServlet配置(web.xml)
<servlet>
<servlet-name>webservice</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>webservice</servlet-name>
<url-pattern>/ws/*</url-pattern>
</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
<?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">
<!-- 把jaxws:client当做一个特殊的bean -->
<jaxws:client id="weatherService" serviceClass="com.itcast.weather.IWeatherService"
address="http://localhost:8080/CXFServer/ws/weather?wsdl">
</jaxws:client>
</beans>
4、测试类Test
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext_cxf.xml");
IWeatherService weatherService = (IWeatherService) context.getBean("weatherService");
String info = weatherService.getWeatherInFoByCityName("北京");
System.out.println(info);
}
}
第二节:手机号码归属地查询
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、创建测试类:
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("classpath:applicationContext_cxf.xml");
MobileCodeWSSoap mobileCode = (MobileCodeWSSoap) app.getBean("code");
String codeInfo = mobileCode.getMobileCodeInfo("1850137", null);
System.out.println(codeInfo);
}
第三节、红日物流BOS系统开发
一、数据库设计-----mysql
运单:
state:
0-待发货 1-已发货 2-已结束
运单明细:
二、代码生成器生成基本代码
三、bos两个服务开发
1、pol.xml配置:参上
2、applicationContext_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 ">
<bean id="waybillService" class="cn.itcast.bos.ws.WayBillService" ></bean>
<jaxws:server address="waybill" >
<jaxws:serviceBean>
<ref bean="waybillService" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
3、web.xml配置:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/admin/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>webservice</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>webservice</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
4、struts.xml配置:(namespace)
<struts>
<package name="default" namespace="/admin" extends="struts-default">
<!-- -->
<action name="waybill_*" class="waybillAction" method="{1}"></action>
<!-- -->
<action name="waybilldetail_*" class="waybilldetailAction" method="{1}"></action>
</package>
</struts>
5、代码:
接口略,@WebService
public class WayBillService implements IWayBillService {
private IWaybilldetailBiz waybilldetailBiz ;
private IWaybillBiz waybillBiz ;
public void setWaybillBiz(IWaybillBiz waybillBiz) {
this.waybillBiz = waybillBiz;
}
public void setWaybilldetailBiz(IWaybilldetailBiz waybilldetailBiz) {
this.waybilldetailBiz = waybilldetailBiz;
}
/**
* 根据运单号获取运单详情
*/
public List<Waybilldetail> getWayBilldetails(Long sn) {
Waybilldetail t1 = new Waybilldetail();
t1.setSn(sn);
List<Waybilldetail> list= waybilldetailBiz.getList(t1, null, null);
return list;
}
/**
* 在线下单预约功能
* state:0待发 1在途 2结束
*/
public Long online(Long userid, String toaddress, String addressee, String tele, String info) {
Waybill waybill = new Waybill();
waybill.setAddressee(addressee);
waybill.setInfo(info);
waybill.setState("0");
waybill.setTele(tele);
waybill.setToaddress(toaddress);
waybill.setUserid(userid);
waybillBiz.add(waybill);
return waybill.getSn();
}
}
第四节:erp中调用BOS系统
在erp的biz层把bos系统的依赖加进来
一、配置文件
CXF配置:
<?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 ">
<bean id="waybillService" class="cn.itcast.bos.ws.WayBillService" >
<property name="waybillBiz" ref="waybillBiz" ></property>
<property name="waybilldetailBiz" ref="waybilldetailBiz" ></property>
</bean>
<jaxws:server address="waybill" >
<jaxws:serviceBean>
<ref bean="waybillService" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
二、erp项目的biz层调用bos逻辑
action:
public void waybilldetials(){
List<Waybilldetail> list = ordersBiz.waybilldetials(sn);
write(JSON.toJSONString(list));
}
biz:
public List<Waybilldetail> waybilldetials(Long sn) {
List<Waybilldetail> list = wayBillService.getWayBilldetails(sn);
return list;
}
注入省略
三、前端
$("#waybillBtn").bind("click",function(){
if($("#sn").html()==null||$("#sn").html()==""){
$.messager.alert("提示","没有物流信息");
}
$("#waybillWindow").window("open");
$("#waybillGrid").datagrid({
url:‘orders_waybilldetials.action?sn=‘+$("#sn").html(),
columns:[[
{field:‘exedate‘,title:‘日期‘,width:100},
{field:‘exetime‘,title:‘时间‘,width:100},
{field:‘info‘,title:‘信息‘,width:100}
]],
singleSelect:true
})
})
第五节、erp中调用在线下单预约服务
出库的逻辑里面添加在线下单功能
biz:
private IWayBillService wayBillService ;
public void setWayBillService(IWayBillService wayBillService) {
this.wayBillService = wayBillService;
}
private ISupplierDao supplierDao;
public void setSupplierDao(ISupplierDao supplierDao) {
this.supplierDao = supplierDao;
}
/**
* 出库逻辑
* @param id
* @param empuuid
* @param storeuuid
*/
public void doOutstore(Long id,Long empuuid,Long storeuuid){
// 1、修改订单项状态
// private java.util.Date endtime;//结束日期
// private Long ender;//库管员
// private Long storeuuid;//仓库编号
// private String state;//状态 采购订单0未入库 1已入库 销售订单 0未出库 1已出库
Orderdetail orderdetail = get(id);
orderdetail.setEnder(empuuid);
orderdetail.setStoreuuid(storeuuid);
orderdetail.setEndtime(new Date());
orderdetail.setState("1");
// 2、修改库存表
// 2.1 先从数据库中查询是否有此商品的记录:传的条件:仓库ID 商品ID
Storedetail t1= new Storedetail();
t1.setGoodsuuid(orderdetail.getGoodsuuid());
t1.setStoreuuid(orderdetail.getStoreuuid());
List<Storedetail> list = storedetailDao.getList(t1, null, null);
// 如果有记录
if (list!=null&&list.size()>0) {
t1=list.get(0); // if(t1.getNum()>=0){ //如果足够出库 数量递减
t1.setNum(t1.getNum()-orderdetail.getNum());
//
// }else{//如果数据不够出库 抛异常
//
// }
if (t1.getNum()<0) {
throw new ErpException("库存不足");
}
}else{
throw new ErpException("这个仓库没有此商品");
}
// 3、添加一个流水记录
// private Long uuid;//编号
// private Long empuuid;//员工编号
// private java.util.Date opertime;//操作日期
// private Long storeuuid;//仓库编号
// private Long goodsuuid;//商品编号
// private Long num;//数量
// private String type;//类型 1采购订单-入库 2销售订单--出库
Storeoper storeoper = new Storeoper();
storeoper.setEmpuuid(empuuid);
storeoper.setGoodsuuid(orderdetail.getGoodsuuid());
storeoper.setNum(orderdetail.getNum());
storeoper.setOpertime(new Date());
storeoper.setStoreuuid(storeuuid);
storeoper.setType("2");
storeoperDao.add(storeoper);
// 4、修改订单状态
// 前提条件:此订单下的所有订单项都已出库才修改订单状态
// select * from orderdetail where ordersuuid=1 and state=0--有数据 表示有未出库的订单项
Orders orders = orderdetail.getOrders();
Orderdetail detail1 =new Orderdetail();
detail1.setState("0");
detail1.setOrders(orders);
long count = orderdetailDao.getCount(detail1, null,null);
if (count==0) {
// private java.util.Date endtime;//结束日期
// private Long ender;//库管员
orders.setEnder(empuuid);
orders.setEndtime(new Date());
orders.setState("3");
// private String state;//订单状态 采购订单 0为审核 1已审核 2已确认 3已结束 销售订单 0未结束 3已结束
Supplier supplier = supplierDao.get(orders.getSupplieruuid());
List<Orderdetail> orderdetails = orders.getOrderdetails();
String info="";
for (Orderdetail detail : orderdetails) {
info+=detail.getGoodsname()+":"+detail.getNum()+" ";
}
Long sn = wayBillService.online(1l, supplier.getAddress(),supplier.getContact() , supplier.getTele(),info);
orders.setWaybillsn(sn);
}
}
第六节、
webservice远程调用技术 cxf封装了webservice
cxf封装的核心内容是xml
webservice在网络传输的内容是xml 网络协议是soap协议====http协议+xml
验证: