一、上线接口(仅管理员)
1. 校验请求参数
2. 判断(测试)接口是否可以调用
- 引入调用接口的客户端(自己写的 SDK)
- 注入客户端实例
- 调用接口
3. 修改数据库中接口的状态
/*** 上线(发布)接口** @param idRequest* @param request* @return*/@PostMapping("/online")@AuthCheck(mustRole = "admin")public BaseResponse<Boolean> onlineInterfaceInfo(@RequestBody IdRequest idRequest,HttpServletRequest request) {if (idRequest == null || idRequest.getId() <= 0) {throw new BusinessException(ErrorCode.PARAMS_ERROR);}// 1. 校验接口是否存在Long id = idRequest.getId();InterfaceInfo oldInterfaceInfo = interfaceInfoService.getById(id);if (oldInterfaceInfo == null) {throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);}// 2. 校验接口是否可以调用com.ghost.leapiclientsdk.model.User user = new com.ghost.leapiclientsdk.model.User();user.setUsername("testInterfaceInfo");String nameByJSON = leAPIClient.getNameByJSON(user);if (StringUtils.isBlank(nameByJSON)) {throw new BusinessException(ErrorCode.SYSTEM_ERROR, "接口调用失败");}// 3. 修改数据库中接口的状态InterfaceInfo interfaceInfo = new InterfaceInfo();interfaceInfo.setId(id);interfaceInfo.setStatus(InterfaceInfoStatusEnum.ONLINE.getValue());boolean result = interfaceInfoService.updateById(interfaceInfo);return ResultUtils.success(result);}
4. 测试上线接口功能
- 使用 Knife4j 接口文档进行测试
- 查看数据库中接口状态是否修改成功
二、下线接口(仅管理员)
1. 校验请求参数
2. 校验接口是否已经发布上线:只有已经上线的接口才能下线
3. 修改数据库中接口的状态
/*** 下线(关闭)接口** @param idRequest* @param request* @return*/@PostMapping("/offline")public BaseResponse<Boolean> offlineInterfaceInfo(@RequestBody IdRequest idRequest, HttpServletRequest request) {if (idRequest == null || idRequest.getId() <= 0) {throw new BusinessException(ErrorCode.PARAMS_ERROR);}// 1. 校验接口是否存在Long id = idRequest.getId();InterfaceInfo oldInterfaceInfo = interfaceInfoService.getById(id);if (oldInterfaceInfo == null) {throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);}// 2. 只有已经上线的接口才能下线if (oldInterfaceInfo.getStatus() != InterfaceInfoStatusEnum.ONLINE.getValue()) {throw new BusinessException(ErrorCode.PARAMS_ERROR, "该接口尚未上线");}// 3. 修改数据库中接口的状态InterfaceInfo interfaceInfo = new InterfaceInfo();interfaceInfo.setId(id);interfaceInfo.setStatus(InterfaceInfoStatusEnum.OFFLINE.getValue());boolean result = interfaceInfoService.updateById(interfaceInfo);return ResultUtils.success(result);}
4. 测试上线接口功能
- 使用 Knife4j 接口文档进行测试
- 查看数据库中接口状态是否修改成功