Java 透傳


這次任務是接口透傳

1 首先外部有一個提供一個完整的接口,可以調的通。

測試時候可以postman調用。外部系統得要提供完成API文檔。以助於更好的對接。

例如確定請求方式,入參的格式。

將外部接口配置

 

 3 調用

 

 這里用的是post請求,返回數據的json格式,已經封裝成result.

@Override
    public Result storeInforMation(StoreInformation storeInformation) throws Exception {
        Result result =new Result();
        List<StoreListinfo> storeListinfo = new ArrayList<>();
        StoreListinfoBound storeListinfoBound=new StoreListinfoBound();
        Map<String, Object> map = new HashMap<>();
        map.put("code", storeInformation.getCode());
        map.put("provinceId", storeInformation.getProvinceId());
        map.put("cityId", storeInformation.getCityId());
        map.put("areaId", storeInformation.getAreaId());
        map.put("agentName", storeInformation.getAgentName());
        String response = HttpClientUtil.httpPostRequest(wmsZngUrl,JSON.toJSONString(map));
        log.info("wmsZngUrl={} and  response is: {}",wmsZngUrl,response);
         com.jiutong.entity.flagshipStore.Result result1 = JSONObject.parseObject(response, com.jiutong.entity.flagshipStore.Result.class);
         if( result1.getCode()==-1){
              result.setResultCode(new ResultCode(-1,"請求失敗"));
         }else{
              JSONObject data=JSONObject.parseObject(String.valueOf(result1.getData()));
             if(data==null){
                 result.setResultCode(new ResultCode(0,"請求成功,暫無數據"));
             }else{
                 Object storeList = Optional.ofNullable(getFromJson(data, "storeList")).orElse("");
                 Object bound = Optional.ofNullable(getFromJson(data, "bound")).orElse("");
                 com.alibaba.fastjson.JSONArray jsonArray= JSON.parseArray(JSONUtil.toJson(storeList));
                 for(int i=0;i<jsonArray.size();i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                     String address = Optional.ofNullable(getFromJson(jsonObject,"address")).orElse("").toString();
                     String storeName = Optional.ofNullable(getFromJson(jsonObject,"storeName")).orElse("").toString();
                     Integer  id = (Integer) Optional.ofNullable(getFromJson(jsonObject,"id")).orElse("");
                     StoreListinfo storeListinfo1 = new StoreListinfo();
                     storeListinfo1.setId(id);
                     storeListinfo1.setStoreName(storeName);
                     storeListinfo1.setAddress(address);
                     storeListinfo.add(storeListinfo1);
                 }
                 storeListinfoBound.setBound(bound.toString());
                 storeListinfoBound.setStoreListinfo(storeListinfo);
                 result.setData(storeListinfoBound);
             }
         }
       return  result;
    }

    private Object getFromJson(JSONObject jsonObject, String key){
        if(jsonObject.containsKey(key)){
            return jsonObject.get(key);
        }
        return null;
    }

 

4

 

 JSON.parseObject(String str)是將str轉化為相應的JSONObject對象


5 HttpClientUtil.httpPostRequest方法

 

 

 

 

 

 

 

任務也是比較簡單的,在聯調的時候,花的時間比較多,兩者很多事情沒有說清楚,需要一個詳細的API文檔。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM