LG CNS 부트캠프 학습일지 28일차
학습 내용
- 블로그 앱 프론트엔드 백엔드 연동
- Filter 와 Interceptor
- AOP (Aspect Oriented Programming)
- Stream API의
peek()메소드
Filter와 Interceptor
각각이 무엇인가도 중요하지만 스프링부트 프레임워크 안에서 어디에 위치해서 흐름을 제어하는지 알면 나중에 JWT를 배울 때 유용할 것 같다. 정리하면 아래와 같다.
1
프론트엔드 - 필터 - 서브릿 - 인터셉터 - 컨트롤러 - ...
- 필터는 프론트엔드와 서브릿 사이에 위치한다.
- 필터는 프론트엔드와 백엔드가 주고받는 데이터의 유효성을 검증하는 등의 역할을 한다.
- 인터셉터는 서브릿과 컨트롤러 사이에 위치한다.
- 인터셉터는 특정 조건에 따라 분기하는데 사용된다.
ChatGPT에게 물어보니 필터와 인터셉터는 더 많은 기능을 제공하는데, 자세한 내용은 실습을 하면서 정리하는 것이 좋아보인다.
AOP (Aspect Oriented Programming)
Aspect Oriented Programming이라는 이름만으로는 어떤 것인지 이해하기 어려웠다. ChatGPT는 이렇게 설명한다.
Aspect-Oriented Programming (AOP) is a programming paradigm that aims to separate cross-cutting concerns from your business logic.
A cross-cutting concern is functionality that affects many parts of the application, such as:
- Logging
- Transaction management
- Security/authorization
- Caching
- Performance monitoring
- Exception handling
비즈니스 로직과 기타 자잘한 작업을 분리시킬 수 있다.
1
2
3
4
5
6
7
8
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(exception = "MethodArgumentNotValidException.class")
public ResponseEntity<?> validate(MethodArgumentNotValidException e) {
// MethodArgumentNotValidException 예외가 발생하면 실행
...
}
}
실습할 때 해결하지 못한 문제가 있었다. GlobalExceptionHandler가 있으면 Swagger UI 에 접근하지 못하는 문제였다. springdoc 버전과 관련이 있는 것 같았다.
1
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0'
Stream API의 peek() 메소드
이런게 있다는 것을 오늘 처음 알았다. 메소드 이름의 사전적 정의대로 스트림 안에 있는 요소들에 영향을 미치지 않으면서 요소들을 가지고 작업을 수행할 수 있다. Stream 객체는 디버거를 사용하기가 어렵다. 하지만 peek() 메소드를 사용하면 중간값을 콘솔에 출력하는데 사용할 수 있다.
1
2
3
4
5
6
7
public void doSomething(List<Pokemon> pokemon) {
pokemon.stream()
.map(Pokemon::getSpecies)
.peek(species -> System.out.println(species.getName()))
.map(Species::getTypes)
.forEach(types -> System.out.println(types))
}
Comments powered by Disqus.