注解忽略Controller、再次讲解跨域问题、日志记录service层运行时间、注解实现mybatis的SQL语句输出

建议在controller层写CRUD时,注明类型,因为在swagger中才好生成对应的测试文档,否则会生成CRUD四个不同的测试文档请求。

当我们的controller层不想被swagger文档测试访问时,可以使用注解:

@ApiIgnore

 关于跨域的问题: 用户登录页面是放在本地的Tocat的目录webapps下的,后台服务是用idea运行的,此时用户登录向idea中的服务发起请求,由于是不同的两个服务器,出于安全的问题考虑,此时就会产生跨域的问题

需要整合自己的日志框架的前提是 剔除掉自身携带的日志框架

spring-boot-starter-logging
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <!--            通过exclusions标签进行部分依赖的排除-->
            <exclusions>
                <!--排除springboot默认使用logging-->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


我们可以通过logger日志来记录每一个service执行的时间,根据不同的执行时间划分不同的警告级别

@Aspect
@Component
public class ServiceLogAspect {

    static final Logger logger = LoggerFactory.getLogger(ServiceLogAspect.class);
    static final long ERRORTIME = 3000;
    static final long WARNTIME = 2000;

    @Around("execution(* com.imooc.service.impl..*.*(..))")
    public Object recodeTimeLog(ProceedingJoinPoint joinPoint) throws Throwable {
        logger.info("======= 开始执行 {}.{} =========", joinPoint.getTarget().getClass(),
                joinPoint.getSignature().getName());

        long begin = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long end = System.currentTimeMillis();
        long takeTime = end - begin;
        if (takeTime > ERRORTIME) {
            logger.error("======= 执行结束,耗时{} 毫秒 ========", takeTime);
        } else if (takeTime > WARNTIME) {
            logger.warn("======= 执行结束,耗时{} 毫秒 ========", takeTime);
        } else {
            logger.info("======= 执行结束,耗时{} 毫秒 ========", takeTime);
        }
        return result;
    }
    
}

猜你喜欢

转载自blog.csdn.net/awodwde/article/details/119545186