spring+webservice使用cxf框架搭建服务端和客户端

  WebService三要素:

  WebService的三要素是:

  SOAP (Simple Object Access Protocol):简易对象访问协议,soap用来描述传递信息的格式。

  WSDL (WebServices Description Language):Web服务描述语言,用来描述如何访问具体的接口。

  UDDI (Universal Description Discovery and Integration):通用描述、发现及整合,用来管理、分发、查询webService。

  1234

  XML+XSD,SOAP和WSDL就是构成WebService平台的三大技术。

  一 搭建服务端

  首先在pom中加入cxf的依赖

  org.apache.cxf

  cxf-rt-frontend-jaxws

  3.1.8

  org.apache.cxf

  cxf-rt-transports-http

  3.1.8

  12345678910

  然后编写需要暴露在外的程序接口:

  接口需要@webservice注解:

  package com.customer.web.ws;

  import java.util.List;

  import javax.jws.WebService;

  import com.customer.entitys;

  import com.customer.entity.Users;

  @WebService

  public interface IShopWS {

  /**

  * 查询未关联定区的店铺

  * @return

  */

  public List queryAllShopsByNotAssociate();

  /**

  * 查询指定定区关联的店铺

  * @param decidedzoneId

  * @return

  */

  public List queryAllShopsByAssociate(int decidedzoneId);

  /**

  * 修改指定定区关联的店铺

  * @param decidedzoneId

  * @param shopIds

  */

  public void updateShopsDecidedzoneId(int decidedzoneId, List shopIds);

  /**

  * 根据电话号码查询指定用户

  * @param phone

  * @return

  */

  public Users queryByPhone(String phone);

  }

  12345678910111213141516171819202222232425262728293031323334353637383940414243

  编写实现类

  @Service("shopWS")

  public class ShopWSImpl implements IShopWS{

  @Autowired

  private ShopsMapper shopMapper;

  @Autowired

  private UsersMapper userMapper;

  @Override

  public List queryAllShopsByNotAssociate() {

  ShopsExample se=new ShopsExample();

  Criteria c1=se.createCriteria().andShopRemarkIsNull();

  Criteria c2=se.createCriteria().andShopAddressEqualTo("");

  se.or(c1);

  se.or(c2);

  return shopMapper.selectByExample(se);

  }

  @Override

  public List queryAllShopsByAssociate(int decidedzoneId) {

  ShopsExample se=new ShopsExample();

  se.createCriteria().andShopRemarkEqualTo(decidedzoneId + "");

  return shopMapper.selectByExample(se);

  }

  @Override

  public void updateShopsDecidedzoneId(int decidedzoneId, List shopIds) {

  List slist=queryAllShopsByAssociate(decidedzoneId);

  for (Shops shop : slist) {

  shop.setShopRemark(null);

  shopMapper.updateByPrimaryKey(shop);

  }

  if(shopIds !=null) {

  for (int i=0; i < shopIds.size(); i++) {

  Shops s=new Shops();

  s.setShopId(shopIds.get(i));

  s.setShopRemark(decidedzoneId + "");

  shopMapper.updateByPrimaryKeySelective(s);

  }

  }

  }

  @Override

  public Users queryByPhone(String phone) {

  UsersExample ue=new UsersExample();

  ue.createCriteria().andUsersTelephoneEqualTo(phone);

  List list=userMapper.selectByExample(ue);

  return list !=null && list.size() > 0 ? list.get(0) : null;

  }

  }

  123456789101112131415161718192022222324252627282930313233343536373839404142434445464748495051525354

  配置文件spring-cxf(需要添加到spring管理)

  xmlns:xsi="w3/2001/XMLSchema-instance"

  xmlns:jaxws="cxf.apache/jaxws"

  xsi:schemaLocation="

  springframework/schema/beans

  springframework/schema/beans/spring-beans.xsd

  cxf.apache/jaxws cxf.apache/schemas/jaxws.xsd">

  //address访问服务端的地址

  1234567891011121314151617

  web.xml需要加入以下配置:

  cxf

  org.apache.cxf.transport.servlet.CXFServlet

  cxf

  /service/*

  现在服务端编写完成:

  访问的方式:ip地址/service/shops?wsdl

  二,编写客户端

  通过cmd中输入:wsimport -s . ip地址/service/shops?wsdl可以获得服务端所暴露的接口,将所需要的接口放入客户端中就可以调用了。

  1.同样需要cxf的pom依赖

  2.编写spring-cxf.xml

  xmlns:xsi="w3/2001/XMLSchema-instance"

  xmlns:p="springframework/schema/p"

  xmlns:aop="springframework/schema/aop"

  xmlns:tx="springframework/schema/tx"

  xmlns:jaxws="cxf.apache/jaxws"

  xmlns:soap="cxf.apache/bindings/soap"

  xmlns:context="springframework/schema/context"

  xsi:schemaLocation="springframework/schema/beans

  springframework/schema/beans/spring-beans.xsd

  springframework/schema/aop

  springframework/schema/aop/spring-aop.xsd

  springframework/schema/tx

  springframework/schema/tx/spring-tx.xsd

  springframework/schema/context

  springframework/schema/context/spring-context.xsd

  cxf.apache/bindings/soap

  cxf.apache/schemas/configuration/soap.xsd

  cxf.apache/jaxws

  cxf.apache/schemas/jaxws.xsd">

  serviceClass="com.lbx.dms.webservice.IShopWS"

  address="ip地址/service/shops">

  12345678910111213141516171819202222232425262728

  一个完整的webservice就搭建完成了。

上一篇:喜讯 Apache APISIX Committer 张晋涛当选中国开源先锋 33 人


下一篇:最详细的Log4j使用教程