카테고리 없음

Spring Boot 일정 관리 트러블 슈팅

mooncommit 2026. 4. 23. 10:20

Trouble Shooting List

  • Port already in use
  • Schedule 생성자에서 값이 null로 저장되는 문제
  • 400 Bad Request
  • author 필드 제거 후 연관관계 적용
  • Schedule 생성 시 userId로 User를 못 찾는 문제
  • 세션 검증 적용 후 userId 제거
  • (User) 형변환(캐스팅) 오류
  • 수정/삭제 시 본인 확인

1. Port already in use

🔴 Problem

Web server failed to start. Port 8081 was already in use.

🔍 Cause

이전에 실행했던 서버가 종료되지 않은 채로 다시 실행하려고 해서 발생

✅ Solution

lsof -i :8081
kill -9 [PID숫자]

2. Schedule 생성자에서 값이 null로 저장되는 문제

🔴 Problem

일정 생성 시 title, content, author가 null로 저장됨

🔍 Cause

Schedule 생성자에서 필드에 값을 할당하지 않음

// 문제 코드
public Schedule(String title, String content, String author) {
    super(); // title, content, author 할당 안 함
}

✅ Solution

public Schedule(String title, String content, String author) {
    this.title = title;
    this.content = content;
    this.author = author;
}

3. 400 Bad Request

🔴 Problem

{
    "status": 400,
    "error": "Bad Request"
}

🔍 Cause

포스트맨에서 GET 요청을 POST로 잘못 보냄

✅ Solution

포스트맨에서 HTTP 메서드를 GET으로 변경


4. author 필드 제거 후 연관관계 적용

🔴 Problem

Lv2에서 Schedule 엔티티의 author(String) 필드를 User 연관관계로 변경하면서 Service, DTO 등 여러 곳에서 오류 발생

🔍 Cause

author 필드를 제거하고 User 객체로 바꾸면서 연관된 코드들을 전부 수정해야 했음

✅ Solution

  • Schedule 엔티티에서 author 제거 후 @ManyToOne 연관관계 추가
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private User user;
  • DTO에서 String authorLong userId로 변경
  • Service에서 userIdUser 조회 후 Schedule 생성

5. Schedule 생성 시 userId로 User를 못 찾는 문제

🔴 Problem

Schedule 생성 시 request.getUserId()User를 찾아야 하는데 UserRepositoryScheduleService에 없어서 오류 발생

✅ Solution

ScheduleServiceUserRepository 추가

private final ScheduleRepository scheduleRepository;
private final UserRepository userRepository; // 추가

6. 세션 검증 적용 후 userId 제거

🔴 Problem

Lv4 로그인 구현 후 기존에 클라이언트가 userId를 직접 보내던 방식이 보안상 위험함

🔍 Cause

클라이언트가 다른 사람의 userId를 넣어서 요청할 수 있음

✅ Solution

userId 대신 세션에서 로그인한 유저를 꺼내서 사용

// Controller
HttpSession session = servletRequest.getSession();
User user = (User) session.getAttribute("loginUser");
scheduleService.save(request, user);
// Service - 기존
User user = userRepository.findById(request.getUserId()).orElseThrow();

// Service - 변경 후
public CreateScheduleResponseDto save(CreateScheduleRequestDto request, User user) { ... }

7. (User) 형변환(캐스팅) 오류

🔴 Problem

session.getAttribute("loginUser") 반환 타입이 Object라서 User 타입으로 바로 사용 불가

✅ Solution

(User)로 캐스팅해서 사용

User user = (User) session.getAttribute("loginUser");

8. 수정/삭제 시 본인 확인

🔴 Problem

로그인한 유저가 다른 사람의 일정을 수정/삭제할 수 있는 문제

✅ Solution

Service에서 로그인한 유저와 일정 작성자의 이메일을 비교

if (!user.getEmail().equals(schedule.getUser().getEmail())) {
    throw new RuntimeException("본인 일정만 수정할 수 있습니다.");
}

😎😎😎
모든 트러블 슈팅 해결 완료⎝⍥⎠