본문 바로가기
TIL

TIL내일배움캠프 15주차 Spring Boot로 REST API와 AOP를 활용한 코드 실행 및 로그 기록

by 율량동박씨 2024. 7. 22.

오늘의 키워드

 

  • Spring Boot
  • REST API
  • Logging
  • AOP (Aspect-Oriented Programming)

오늘의 내용

  • AOP를 사용해 메서드 실행시간을 측정
  • 코드를 입력받고 실행시키는 Controller 구현
//예시코드

@RestController
public class CodeExecutionController {

    @PostMapping("/execute")
    public ResponseEntity<String> executeCode(@RequestBody String code) {
        String result = runCode(code);
        return ResponseEntity.ok(result);
    }

    private String runCode(String code) {
        return "Executed: " + code;
    }
}
  • AOP를 사용해 코드 실행 메서드의 실행 시간을 측정
@Aspect
@Component
public class ExecutionTimeAdvice {

    private static final Logger logger = LoggerFactory.getLogger(ExecutionTimeAdvice.class);

    @Around("@annotation(org.springframework.web.bind.annotation.PostMapping) && execution(* com.example.demo..*(..))")
    public Object measureExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        long executionTime = System.currentTimeMillis() - start;
        logger.info(joinPoint.getSignature() + " executed in " + executionTime + "ms");
        return proceed;
    }
}

 

오늘의 회고

 최종프로젝트를 내부회의를 거쳐서 초반 작업을 시작했는데 튜터님의 피드백으로 너무 단순한 로직만 있는 거 같다는 의견이 있어 새로운 아이디어를 받아 팀원들과 회의 끝에 도입하고 어떤 방식으로 진행할지 결정되었는데 잘 되겠지 어떤 프로젝트인지는 아직 올리면 안 될 거 같다