这次任务是接口透传
1 首先外部有一个提供一个完整的接口,可以调的通。
测试时候可以postman调用。外部系统得要提供完成API文档。以助于更好的对接。
例如确定请求方式,入参的格式。
2
将外部接口配置
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文档。