通用回调接口应用

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(new HttpResult(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; } }

 

 

 

posted on 2020-12-02 15:47  jped  阅读(182)  评论(0编辑  收藏  举报