我们在开发时候,程序中有时候可能需要访问已经提供的外部接口,这里使用retrofit访问。具体代码已经上传到github和gitlab。
这里是controller层调用例子。
@Role(roles = {RoleEnum.SUPER_ADMIN, RoleEnum.ADMIN, RoleEnum.OPERATOR}) @RequestMapping(value = "lockAction_CancelUnlockAuthority", method = RequestMethod.GET) public Map<String, Object> CancelUnlockAuthority(@RequestParam Integer userId, @RequestParam String userPhone, @RequestParam Integer hotelId, @RequestParam Integer buildingId, @RequestParam Integer roomId) { LockInfo lockInfo = new LockInfo(hotelId.toString(), buildingId.toString(), roomId.toString()); UserInfo userInfo = new UserInfo(userPhone, userId.toString(), lockInfo); Call<Map<String, Object>> mapCall = this.lockService.CancelUnlockAuthority(userInfo); return this.execute(mapCall); } private Map<String, Object> execute(Call<Map<String, Object>> unlock) { Response<Map<String, Object>> execute = null; try { execute = unlock.execute(); } catch (IOException e) { throw new ProjectException(ExceptionCode.LOCK_SERVER_ERROR.getCode(), ExceptionCode.LOCK_SERVER_ERROR.getMessage()); } Map<String, Object> body = execute.body(); if (body == null) { throw new ProjectException(ExceptionCode.LOCK_SERVER_ERROR.getCode(), ExceptionCode.LOCK_SERVER_ERROR.getMessage()); } Object isError = body.get("isError"); if (isError != null && isError instanceof Boolean && !((Boolean) isError).booleanValue()) { return ResponseMap.success("success").data("data", body.get("data")).result(); } else { throw new ProjectException(ExceptionCode.LOCK_SERVER_ERROR.getCode(), body.get("errorMessage") == null ? ExceptionCode.LOCK_SERVER_ERROR.getMessage() : body.get("errorMessage").toString()); } }