通用回撥介面應用
阿新 • • 發佈:2020-12-02
Controller類:
/** * CallbackController: * 回撥介面*/ @RestController @RequestMapping(value = "callback") @Api(value = "回撥介面", tags = "回撥介面") public class CallbackController { private final String MODULE = "回撥介面"; private static final Logger logger = LoggerFactory.getLogger(CallbackController.class); @Resource private CallbackStrategy callbackStrategy; /** * 回撥介面接收資料 * @param callbackData * @param request * @param response * @return * @throws Exception */ @ApiOperation(value = "回撥介面接收資料", notes = "回撥介面接收資料") @ApiParam(type = "CallbackData", name = "callbackData", value = "接收資料", required = true) @PostMapping(value = "/pushData") public ResponseEntity<HttpResult> addSubmit( @RequestBody CallbackData callbackData, HttpServletRequest request, HttpServletResponse response) throws Exception{ return new ResponseEntity<>(callbackStrategy.getResource(newHttpResult(request.getRequestURI()),callbackData), HttpStatus.OK); } }
實體類:
public class CallbackData { /** * 回撥型別 */ private String type; /** * 回撥資料 */ private String data; public CallbackData(String type, String data) { this.type = type; this.data = data; } public CallbackData() {} public String getType() { return type; } public void setType(String type) { this.type = type; } public String getData() { return data; } public void setData(String data) { this.data = data; } }
CallbackStrategy類:
* 通過Spring將實現Strategy的實現類都自動注入到strategyMap類中
* 當用戶傳入選擇的資源池時,自動根據 type 去對應的資源池實現中查詢資源。
* 所以傳參裡面必須要有一個type型別(此處的型別應該為monitorAnchorStatus),傳參請看上篇內容
@Service public class CallbackStrategy { @Resource private final Map<String, StrategyService> strategyMap = new ConcurrentHashMap<>(); public CallbackStrategy(Map<String, StrategyService> strategyMap) { strategyMap.forEach(this.strategyMap::put); } public HttpResult getResource(HttpResult result, CallbackData callbackData){ if (!strategyMap.containsKey(callbackData.getType())) { throw new CheckException("回撥型別未找到,無法處理推送資料."); } return strategyMap.get(callbackData.getType()).handle(result,new String(Base64.getDecoder().decode(callbackData.getData()), StandardCharsets.UTF_8)); } }
StrategyService類:
public interface StrategyService { /** * 處理資料 * @param result * @param data * @return */ HttpResult handle(HttpResult result, String data); }
對應的一個實現類:
@Component("monitorAnchorStatus") public class MonitorAnchorStatusStrategyServiceImpl implements StrategyService { @Resource private MonitorAnchorService monitorAnchorService; @Resource private MonitorObjectService monitorObjectService; @Override public HttpResult handle(HttpResult result, String data) {
//引數解析 MonitorAnchorStatusDO aDo= JSON.parseObject(data, MonitorAnchorStatusDO.class); TPmMonitorAnchorExample anchorExample=new TPmMonitorAnchorExample(); anchorExample.createCriteria().andAnchorArchivesCodeEqualTo(aDo.getAnchorArchivesCode()); anchorExample.setOrderByClause("anchor_archives_code asc limit 1"); List<TPmMonitorAnchor> anchorList=monitorAnchorService.selectByExample(anchorExample); if (null==anchorList || anchorList.isEmpty()){ throw new CheckException("xxxx"); } TPmMonitorAnchor anchor=anchorList.get(0); if(null!=aDo.getEarlyWarningLevelId()){ anchor.setEarlyWarningLevelId(aDo.getEarlyWarningLevelId()); } if(null!=aDo.getAnchorStatus()){ anchor.setAnchorStatus(aDo.getAnchorStatus()); } if(null!=aDo.getIsOffline()) { anchor.setIsOffline(aDo.getIsOffline()); } monitorAnchorService.updateByPrimaryKey(anchor); monitorObjectService.changeMonitorObjectMonitorSituation(anchor.getMonitorObjectId()); result.setMessage("xxx."); result.setStatus(HttpStatus.OK.value()); return result; } }