[Spring] Http 요청 처리 - Controller, Service, Repository,(Day 53)

코딩기록·2024년 12월 23일

[🌐 FYI. 서버 <-> 프론트 통신 기본 원리]

[🧑‍💻Controller]

  • 사용자 입력값의 기본적인 검증을 수행
  • Service 계층으로 비즈니스 로직 처리 위임

[⚙️Service]

  • 핵심 비즈니스 로직을 구현
//  @Service 라고 하면 @Component 처럼 객체 생성을 Spring 에 위임함
@Service
public class UserService {
    private final UserRepository userRepository;

    public User findUser(Long id) {
        // 비즈니스 로직 수행
        User user = userRepository.findById(id)
            .orElseThrow(() -> new UserNotFoundException(id));

        // 추가적인 비즈니스 규칙 적용
        if (!user.isActive()) {
            throw new InactiveUserException(id);
        }

        return user;
    }
}

[🗂️Repository]

  • CRUD(Create, Read, Update, Delete) 작업을 수행
  • 여기에서 DB와 연결

0개의 댓글