一个简单的外部系统调用接口日志记录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<P, R> {

    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<JSONObject, ResultVO> {
    @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<JSONObject, ResultVO>{
    @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;
    }
}

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在FastAdmin中,可以通过自定义拦截器来记录调用接口日志。 1. 创建一个实现了HandlerInterceptor接口的拦截器类,例如: ``` import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Component public class ApiLogInterceptor implements HandlerInterceptor { private static final Logger logger = LoggerFactory.getLogger(ApiLogInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { logger.info("API request: {} {}", request.getMethod(), request.getRequestURI()); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // do nothing } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { logger.info("API response: {} {}", response.getStatus(), response.getContentType()); } } ``` 这里的拦截器记录了API请求的方法、URI等信息,并在请求完成后记录了响应的状态码和内容类型。 2. 在配置文件中配置拦截器,例如: ``` spring.mvc.interceptors=org.example.ApiLogInterceptor ``` 这里配置了ApiLogInterceptor拦截器。 3. 可以使用Slf4j等日志框架来记录日志,也可以使用logback等日志框架的配置文件来配置日志输出位置、格式等信息。 这样,就可以单独记录调用接口日志了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值