一个简单的外部系统调用接口日志记录demo

一、实现思想

抽取接口共方法,作为抽象类。然后不同业务实现类继承此抽象类,实现具体业务。

分析可知公共部分就是将外系统入参接口返回参数记录到数据库,将其抽取出来,作为基础抽象类的公共方法,业务类继承此抽象类,使得不用在每一个业务实现类里面重复造轮子。

二、代码

1、基础抽象类代码demo

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xpf.BrandEnum;
import com.xpf.entity.InputAPILog;
import com.xpf.entity.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public abstract class AbstractIntoLogImpl {

    public R intoLog(P p){
        //拿到入参
        InputAPILog inputAPILog = new InputAPILog();
        inputAPILog.setCreateTime(new Date());
        inputAPILog.setVersion(1);
        inputAPILog.setInputParams(JSON.toJSONString(p));
        inputAPILog.setBarnd(getBrand().getBrandName());
        R r = null;
        try{
            //执行业务
            r = doAction(p);
            inputAPILog.setOutParams(JSON.toJSONString(r));
            System.out.println(inputAPILog);
        }catch (Exception e){
            inputAPILog.setVersion(0);
            inputAPILog.setUpdateTime(new Date());
            log.error("执行API出错:{}", e.getMessage());
            //记得把错误继抛出
            throw e;
        }
        return r;
    }

    public abstract BrandEnum getBrand();

    //执行真正的业务
    public abstract R doAction(P p);
}

2、继承此基础抽象类的业务实现类demo

import com.alibaba.fastjson.JSONObject;
import com.xpf.BrandEnum;
import com.xpf.entity.ResultVO;
import org.springframework.stereotype.Service;

@Service
public class BMWapiServiceImpl extends AbstractIntoLogImpl {
    @Override
    public BrandEnum getBrand() {
        return BrandEnum.BMW;
    }

    @Override
    public ResultVO doAction(JSONObject jsonObject) {
        String bmw = (String) jsonObject.get("BMW");
        return ResultVO.success(bmw);
    }
}

3、写一个controller调用此业务类

import com.alibaba.fastjson.JSONObject;
import com.xpf.entity.ResultVO;
import com.xpf.service.impl.AMGapiServiceImpl;
import com.xpf.service.impl.BMWapiServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/API")
public class TestController {

    @Autowired
    private BMWapiServiceImpl bmWapiService;

    @Autowired
    private AMGapiServiceImpl amGapiService;

    @PostMapping("/getBMW")
    public ResultVO getBMW(@RequestBody JSONObject jsonObject){
        return bmWapiService.intoLog(jsonObject);
    }

    @PostMapping("/getAMG")
    public ResultVO getAMG(@RequestBody JSONObject jsonObject){
        return amGapiService.intoLog(jsonObject);
    }

}

(另一个AMG业务实现类也类似,写出来比较比较)

import com.alibaba.fastjson.JSONObject;
import com.xpf.BrandEnum;
import com.xpf.entity.ResultVO;
import org.springframework.stereotype.Service;

@Service
public class AMGapiServiceImpl extends AbstractIntoLogImpl{
    @Override
    public BrandEnum getBrand() {
        return BrandEnum.AMG;
    }

    @Override
    public ResultVO doAction(JSONObject jsonObject) {
        return ResultVO.success(jsonObject.get("AMG"));
    }
}

对,顺带把 ResultVO 统一返回类粘出如下

import java.io.Serializable;

public class ResultVO implements Serializable {
    private static final long serialVersionUID = -545712146633028245L;
    private boolean success;
    private String code;
    private String message;
    private Object data;

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return this.data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public ResultVO() {
        this.success = false;
    }

    public ResultVO(boolean success) {
        this.success = success;
    }

    public ResultVO(boolean success, Object data) {
        this.success = success;
        this.data = data;
    }

    public ResultVO(boolean success, String message, Object data) {
        this.success = success;
        this.message = message;
        this.data = data;
    }

    public static ResultVO success() {
        ResultVO resultVO = new ResultVO();
        resultVO.setSuccess(true);
        resultVO.setCode("200");
        resultVO.setMessage("");
        resultVO.setData((Object)null);
        return resultVO;
    }

    public static ResultVO success(String errorCode, String errMsg) {
        ResultVO resultVO = new ResultVO();
        resultVO.setSuccess(true);
        resultVO.setCode(errorCode);
        resultVO.setMessage(errMsg);
        resultVO.setData((Object)null);
        return resultVO;
    }

    public static ResultVO success(Object data) {
        ResultVO resultVO = new ResultVO();
        resultVO.setSuccess(true);
        resultVO.setCode("200");
        resultVO.setMessage("");
        resultVO.setData(data);
        return resultVO;
    }

    public static ResultVO serverErr(String errMsg) {
        ResultVO resultVO = new ResultVO();
        resultVO.setSuccess(false);
        resultVO.setCode("500");
        resultVO.setMessage(errMsg);
        resultVO.setData((Object)null);
        return resultVO;
    }

    public static ResultVO serverErr(String errorCode, String errMsg) {
        ResultVO resultVO = new ResultVO();
        resultVO.setSuccess(false);
        resultVO.setCode(errorCode);
        resultVO.setMessage(errMsg);
        resultVO.setData((Object)null);
        return resultVO;
    }
}

三、一个简单的外部系统调用接口日志记录就做好了,开测

一个简单的外部系统调用接口日志记录demo_第1张图片

你可能感兴趣的:(自用工具类,java,开发语言)