美文网首页
HAP_hap调用soap接口

HAP_hap调用soap接口

作者: 灯下驼贼 | 来源:发表于2018-09-26 12:04 被阅读0次

接口地址:


image.png

hap定义接口:


image.png

接口:


public interface IInstorageTrxService extends IBaseService<InstorageTrx>, ProxySelf<IInstorageTrxService> {

   /**
    * Hap调用SOAP接口
    */
   // public ResponseData getTxns(IRequest iRequest,InstorageTrx dto, String date_from, String date_to,int page,int pageSize) throws Exception;
   public List<Object> getTxns(String date_from, String date_to) throws Exception;

   /**
    * 保存远程数据
    *
    * @param dtoList
    */
   void saveInstorageTrx(List<Object> dtoList) throws Exception;

   /**
    * 自定义查询/导出
    */
   List<InstorageTrx> selectInstorageTrx(IRequest iRequest, InstorageTrx dto, int page, int pageSize);

}

构建请求报文,你也可以不用分离出来,直接写在下面的接口实现类中

package SF.xxfnd.contants;

/**
 * Created by General on 10/26/2017.
 */
public class MaterialTxnsConstant {
    private static String PARA_XML = "<pub:item>\n" +
            "                  <pub:name>p_date_from</pub:name>\n" +
            "                  <pub:dataType>STRING</pub:dataType>\n" +
            "                  <pub:values> \n" +
            "                     <pub:item>$P_DATA_FROM$</pub:item>\n" +
            "                  </pub:values>\n" +
            "                </pub:item>\n" +
            "                \n" +
            "                <pub:item>\n" +
            "                  <pub:name>p_date_to</pub:name>\n" +
            "                  <pub:dataType>STRING</pub:dataType>\n" +
            "                  <pub:values> \n" +
            "                     <pub:item>$P_DATA_TO$</pub:item>\n" +
            "                  </pub:values>\n" +
            "                </pub:item>";


    public static String getParaXml(String p_data_from, String p_data_to) {
        String para = PARA_XML;
        para = para.replace("$P_DATA_FROM$", p_data_from);
        para = para.replace("$P_DATA_TO$", p_data_to);
        return para;
    }
}

接口实现:


@Service
@Transactional(rollbackFor = Exception.class)
public class InstorageTrxServiceImpl extends BaseServiceImpl<InstorageTrx> implements IInstorageTrxService {

    @Resource(name = "soapBean")
    private IHapApiService soapService;
    @Autowired
    private IHapInterfaceHeaderService headerService;
    @Autowired
    private ISoapDepartmentLookupService iSoapDepartmentLookupService;
    @Autowired
    InstorageTrxMapper instorageTrxMapper;

    private static Logger logger = LoggerFactory.getLogger(InstorageTrxServiceImpl.class);

    /**
     * soap接口获取远程系统数据
     * @return
     * @throws Exception
     */
    @Override
    public List<Object> getTxns(String date_from,String date_to) throws Exception {

        ResponseData responseData = new ResponseData();
        JSONObject params = new JSONObject();
        params.put("pub:parameterNameValues", MaterialTxnsConstant.getParaXml(date_from, date_to));
       // params.put("pub:parameterNameValues", MaterialTxnsConstant.getParaXml("2018-04-01", "2018-05-01"));
        JSONObject Response_Json = null;
        JSONObject Envelope = null;
        JSONObject EnvBody = null;
        JSONObject runReportResponse = null;
        JSONObject runReportReturn = null;
        String reportdata = null;
        HapInterfaceHeader hapInterfaceHeader = this.headerService.getHeaderAndLine("XxInvMaterialTxnsIntg", "Sync_Item_REPORT");
        try {
            Response_Json = this.soapService.invoke(hapInterfaceHeader, params);
            Envelope = Response_Json.getJSONObject("soapenv:Envelope");
            EnvBody = Envelope.getJSONObject("soapenv:Body");
            runReportResponse = EnvBody.getJSONObject("runReportResponse");
            runReportReturn = runReportResponse.getJSONObject("runReportReturn");
            reportdata = runReportReturn.getString("reportBytes");
            reportdata = new String(Base64.decodeBase64(reportdata.getBytes()), "UTF-8");
        }catch (Exception e) {
            //e.printStackTrace();
            throw e;
        }
        //我的方法中没用到"G_1"这个参数,我是从xml的根元素开始遍历,然后去掉不用的元素
        return (XmlToList.Transform(reportdata,"G_1", InstorageTrx.class));
    }

    /**
     * 保存ERP远程数据
     * @param dtoList
     */
    @Override
    public void saveInstorageTrx(List<Object> dtoList) throws Exception{
        Iterator it = dtoList.iterator();
        while(it.hasNext()) {
            InstorageTrx obj = (InstorageTrx) it.next();
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            obj.setExpireTime(sdf.parse(sdf.format(obj.getExpireTime().getTime() - 1*8*3600*1000L)));
//            obj.setInstorageTime(sdf.parse(sdf.format(obj.getInstorageTime().getTime() - 1*8*3600*1000L)));
            //判断记录是否已经存在
            if(instorageTrxMapper.select(obj).size()<1) {
                obj.setSyncFlag("N");
                obj.setObjectVersionNumber(1L);
                obj.setCreatedBy(-1L);
                obj.setLastUpdatedBy(-1L);
                obj.setCreationDate(new Date());
                obj.setLastUpdateDate(new Date());
                instorageTrxMapper.insert(obj);
            }
        }
    }

    @Override
    public List<InstorageTrx> selectInstorageTrx(IRequest iRequest,InstorageTrx dto,int page,int pageSize) {
        PageHelper.startPage(page, pageSize);
        return instorageTrxMapper.selectInstorageTrx(dto);
    }
}

这样我们就可以拿到返回xml数据,下面我们要解析xml,把xml转为一个给bean。思路是:xml--list<map<>>--list<bean>

package SF.xxfnd.contants;

import SF.xxinv.dto.InstorageTrx;
import org.dom4j.*;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class XmlToList {
   private static Logger logger = LoggerFactory.getLogger(XmlToList.class);

   public static List<Object> Transform(String reportdata, String roots, Object object) throws Exception {
       List<Map<String, Object>> list = new ArrayList();
       Document doc = DocumentHelper.parseText(reportdata);
       Element root = doc.getRootElement();
       list = parse(root, list);
       return getBeans(list, InstorageTrx.class);
   }

   public static List<Map<String, Object>> parse(Element ele, List<Map<String, Object>> list) {
       List el = ele.elements();
       //遍历每个子元素
       Map<String, Object> map = new HashMap();
       boolean isAdd = true;
       for (Object e : el) {
           Element element = (Element) e;
           //如果该元素的内容不是只包含字符串
           if (!element.isTextOnly()) {
               parse(element, list);
           }//递归
           else {
               if ((!element.getQName().getName().equals("P_DATE_FROM")) && ((!element.getQName().getName().equals("P_DATE_TO")))) {
                  // map.put(toHump(element.getQName().getName()), element.getText());
                   map.put(element.getQName().getName(), element.getText());
                   isAdd = true;
               } else {
                   isAdd = false;
               }
           }
       }
       if (isAdd) {
           list.add(map);
       }
       return list;
   }

   //转换为实体类存放到List里面
   public static List<Object> getBeans(List<Map<String, Object>> list, Class<?> clazz) throws Exception {
       Object obj1 = null;
       List<Object> list1 = new ArrayList<Object>();
       for (int i = 0; i < list.size(); i++) {
           Map<String, Object> stringObjectMap = list.get(i);
           logger.info("getBeans_stringObjectMap:" + stringObjectMap);
           Object obj = null;
           try {
               obj = clazz.newInstance();
           } catch (InstantiationException e) {
               e.printStackTrace();
           } catch (IllegalAccessException e) {
               e.printStackTrace();
           }
           obj1 = mapToInstorageTrx(stringObjectMap, clazz);
           list1.add(obj1);
       }
       return list1;
   }
   //将map转为dto
   public static InstorageTrx mapToInstorageTrx(Map<String, Object> map, Class<?> beanClass) throws Exception {
       InstorageTrx instorageTrx = new InstorageTrx();
       instorageTrx.setOtherCost((map.get("OTHER_COST").equals("")) ? null : Double.parseDouble(String.valueOf(map.get("OTHER_COST"))));
       instorageTrx.setExpressNo((String) map.get("EXPRESS_NO"));
       instorageTrx.setWlCompany((String) map.get("WL_COMPANY"));
       instorageTrx.setInstorageRemark((String) map.get("INSTORAGE_REMARK"));
       instorageTrx.setFreight((map.get("FREIGHT").equals("")) ? null : Double.parseDouble(String.valueOf(map.get("FREIGHT"))));
       instorageTrx.setProcureCost((map.get("PROCURE_COST").equals("")) ? null : Double.parseDouble(String.valueOf(map.get("PROCURE_COST"))));
       instorageTrx.setInstorageReason((String) map.get("INSTORAGE_REASON"));
       instorageTrx.setInstorageNo((String) map.get("INSTORAGE_NO"));
       instorageTrx.setProductitemNo((String) map.get("PRODUCTITEM_NO"));
       instorageTrx.setFreightAvgway((String) map.get("FREIGHT_AVGWAY"));
       instorageTrx.setInstorageNum((map.get("INSTORAGE_NUM").equals("")) ? null : Long.parseLong(String.valueOf(map.get("INSTORAGE_NUM"))));
       instorageTrx.setInstorageTime((map.get("INSTORAGE_TIME").equals("")) ? null : stringToDate((String) map.get("INSTORAGE_TIME")));
       instorageTrx.setExpireTime((map.get("EXPIRATION_DATE").equals("")) ? null : stringToDate((String) map.get("EXPIRATION_DATE")));
       instorageTrx.setPactTotalamount((map.get("PACT_TOTALAMOUNT").equals("")) ? null : Double.parseDouble(String.valueOf(map.get("PACT_TOTALAMOUNT"))));
       instorageTrx.setOutPactno((String) map.get("OUT_PACTNO"));
       instorageTrx.setCost((map.get("COST").equals("")) ? null : Double.parseDouble(String.valueOf(map.get("COST"))));
       instorageTrx.setOrganizationName((String) map.get("ORGANIZATION_NAME"));
       instorageTrx.setListSource((String) map.get("LIST_SOURCE"));
       instorageTrx.setDeliveryNo((String) map.get("DELIVERY_NO"));
       instorageTrx.setBatch((String) map.get("BATCH"));
       instorageTrx.setSupplierNo((String) map.get("SUPPLIER_NO"));
       instorageTrx.setSourceNo((String) map.get("SOURCE_NO"));
       instorageTrx.setImportSign((String) map.get("IMPORT_SIGN"));
       instorageTrx.setInstorageType((String) map.get("INSTORAGE_TYPE"));
       instorageTrx.setBarCode((String) map.get("BAR_CODE"));
       instorageTrx.setItemCost((map.get("ITEM_COST").equals("")) ? null : Double.parseDouble(String.valueOf(map.get("ITEM_COST"))));
       instorageTrx.setLocationNo((String) map.get("LOCATION_NO"));
       instorageTrx.setStorageNo((String) map.get("STORAGE_NO"));
       instorageTrx.setPayerFreight((String) map.get("PAYER_FREIGHT"));
       instorageTrx.setIsconfirm((String) map.get("ISCONFIRM"));
       instorageTrx.setItemInstorageNo((String) map.get("ITEM_INSTORAGE_NO"));
       instorageTrx.setItemStorageNo((String) map.get("ITEM_STORAGE_NO"));

       return instorageTrx;
   }

   //字符串转日期
   public static Date stringToDate(String s) throws Exception {
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       Date date = sdf.parse(s);
       return date;
   }
}

备注:报文:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pub="http://xmlns.oracle.com/oxp/service/PublicReportService">
      <soapenv:Header/>
   <soapenv:Body>
      <pub:runReport>
         <pub:reportRequest>
            <pub:attributeLocale>en-US</pub:attributeLocale>
            <pub:attributeTemplate>Default</pub:attributeTemplate>
               <pub:parameterNameValues>
                  <pub:item>
                   <pub:name>p_date_from</pub:name>
                      <pub:dataType>STRING</pub:dataType>                    
                     <pub:values>
                        <pub:item>2017-12-25 00:00:00</pub:item>
                     </pub:values>
                  </pub:item>
                       <pub:item>
                     <pub:name>p_date_to</pub:name>
                      <pub:dataType>STRING</pub:dataType>                     
                     <pub:values>
                        <pub:item>2017-12-30 00:00:00</pub:item>
                     </pub:values>
                  </pub:item>
               </pub:parameterNameValues>
                    <pub:reportAbsolutePath>/Custom/External Web Service Interface/Inventory Transaction/Inventory Transaction Report.xdo</pub:reportAbsolutePath>
                    </pub:reportRequest>
         <pub:userID>xxxxx</pub:userID>
         <pub:password>xxxxx</pub:password>
      </pub:runReport>
   </soapenv:Body>
</soapenv:Envelope>

相关文章

网友评论

      本文标题:HAP_hap调用soap接口

      本文链接:https://www.haomeiwen.com/subject/stubnftx.html