java后端开发:实现服务之间的接口对接

java后端开发:实现服务之间的接口对接

背景

在当前所在项目中,需要进行不同服务之间的数据同步,在日常开发中是比较常见的需求。java和.Net在这方面的整体思路是一致的。特别在此记录。

实现外部接口

步骤

1.编写数据提供方接口;

2.编写数据请求方接口;

3.数据请求方想数据提供方发起web请求,在数据请求方接收到返还结果后,落库。

核心点

1.保证数据请求方的请求结构体和数据提供方的请求结构体一致;

2.保证数据请求方的返回结构体和数据提供方的返回结构体一致;

3.保证web请求的地址和参数配置正确;

实际代码

数据提供方
controller
@RestController
@RequestMapping("/fsDataApi")
@Api(tags = "数据同步接口")
@ResponseResult
public class FsPcdetailApi {

    @Autowired
    private FsPcdetailService fsPcdetailService;

    @ApiOperation("获取平仓明细")
    @PostMapping("getPcdetail")
    public List<FSRMPcdetail> GetPcdetail(@RequestBody ZhonglvReqParam param){
        return fsPcdetailService.GetPcdetail(param.getSettlementDate(),param.getSettlementDate());
    }
}
请求结构
@Data
@ApiModel("入参")
public class ZhonglvReqParam {
    private String settlementDate;
    private String portfolioId;
}
返回结构
package com.kingstar.hedgingcustomer.entity.futureStock;

import io.swagger.annotations.ApiModel;
import lombok.Data;

import java.math.BigDecimal;

@Data
@ApiModel(value = "明细表")
public class FSRMPcdetail {

    private String bsPcdetailId;
    private String fundname;
    private String tradeDate;
    private String mkcode;
    private String stkcode;
	
    public String getFundname(){
        return this.fundname;
    }

    public void setFundname(String fundname){
        this.fundname = fundname;
    }

    public String getTradeDate(){
        return this.tradeDate;
    }

    public void setTradeDate(String tradeDate){
        this.tradeDate = tradeDate;
    }

    public String getMkcode(){
        return this.mkcode;
    }

    public void setMkcode(String mkcode){
        this.mkcode = mkcode;
    }

    public String getStkcode(){
        return this.stkcode;
    }

    public void setStkcode(String stkcode){
        this.stkcode = stkcode;
    }
}
数据请求方
controller

@Slf4j
@Component
@RegistryServiceBean
@SuppressWarnings({"rawtypes", "unchecked"})
public class ServiceFSSE05 {

    @Autowired
    private FSBSPcdetailBiz fSBSPcdetailBiz;

    @Autowired
    private FSBSPcDetailSendBiz fSBSPcDetailSendBiz;

    @RegistryService(serviceId = "0601", serviceDesc = "同步明细")
    public EiInfo getPcDetail(EiInfo in) {
        String messageBody = null;
        List<String> recordList = null;
        try {
            messageBody = in.getString(EiConstant.eaiMessageBody);
            recordList = Arrays.asList(messageBody.split(","));
        } catch (Exception e) {
        }

        if (recordList == null || recordList.isEmpty()) {
            LocalDate currentDate = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
            recordList = new ArrayList<>();
            recordList.add(currentDate.format(formatter));
        }

        List<FSBSPcdetail> pcDetailList = new ArrayList<>();

        for (String settlementDate : recordList) {

            FSBSPositionShareSendReq fsbsPositionShareSendReq = new FSBSPositionShareSendReq();
            fsbsPositionShareSendReq.setSettlementDate(settlementDate);
            // 发送请求
            Result<List<FSBSPcdetail>> res = fSBSPcDetailSendBiz.getPcDetailList(fsbsPositionShareSendReq);
            if (res == null || res.getData() == null) {
                continue;
            }
            pcDetailList.addAll(res.getData());
        }

        saveData(pcDetailList);
        Result<List<FSBSPcdetail>> result = new Result<>();
        result.setSuccess(true);
        result.setData(pcDetailList);
        // 返回发送结果
        EiInfo out = EiInfoResultUtils.fromResult(result);
        out.addRows(EiConstant.resultBlock, Lists.newArrayList(result.getData()));
        return out;
    }

    private void saveData(List<FSBSPcdetail> pcdetailList) {
        BaseUserInfo userInfo = new BaseUserInfo();
        for (FSBSPcdetail pcdetail : pcdetailList) {
            Result<FSBSPcdetailQueryRes> dataEst = fSBSPcdetailBiz.getDetail(pcdetail.getBsPcdetailId());
            FSBSPcdetailSaveReq req = convertData2Req(pcdetail);
            if (dataEst.isSuccess() && dataEst.getData() != null && dataEst.getData().getBsPcdetailId() != null) {
                fSBSPcdetailBiz.doUpdate(req, userInfo);
            } else {
                fSBSPcdetailBiz.doInsert(req, userInfo);
            }
        }
    }

    private FSBSPcdetailSaveReq convertData2Req(FSBSPcdetail input) {
        FSBSPcdetailSaveReq req = new FSBSPcdetailSaveReq();
        req.setFundid(input.getFundid());
        req.setFundname(input.getFundname());
        req.setTradeDate(input.getTradeDate());
        req.setMkcode(input.getMkcode());
        req.setStkcode(input.getStkcode());
        return req;
    }

}
请求结构

同提供方 FSBSPcdetail

返回结构

同提供方 FSBSPositionShareSendReq

Web请求
    @Override
    public Result<List<FSBSPcdetail>> getPcDetailList(FSBSPositionShareSendReq sendReq) {
        Result result = ICRestTemplateUtil.doPost4ICIM(FSSEUrlConstants.GET_FS_FUTURE_PCDETAIL_URL, sendReq, Result.class);
        JSONObject postResult = (JSONObject) JSONObject.parse((String) result.getData());
        List<FSBSPcdetail> list = JSONArray.parseArray(JSON.toJSONString(postResult.get("data")), FSBSPcdetail.class);
        result.setData(list);
        return result;
    }

结语

特别再次记录一下,方便后人。

上一篇:Linux安装mysql【超详细】


下一篇:kafka消费者组分区分配实战