목차
Lv.7 예외 처리 고민
Lv.5~6 댓글 필드명의 뒤죽박죽 현상
Lv.2 시작을 못하겠음
문제 1번: 3 Layer Architecture(Controller, Service, Repository)를 적절히 적용했는지 확인해 보고, 왜 이러한 구조가 필요한지 작성해 주세요.
문제 2번: @RequestParam, @PathVariable, @RequestBody가 각각 어떤 어노테이션인지, 어떤 특징을 갖고 있는지 작성해 주세요.
// 400 번대로 안나오고 500으로만 나와서 그냥 @Colum으로 작성
private void validateTitle(String title) {
if (title == null || title.isBlank()) {
throw new IllegalArgumentException("일정 제목은 필수입니다.");
}
if (title.length() > 30) {
throw new IllegalArgumentException("일정 제목은 30자 이내로 입력해주세요.");
}
}
원래 주석 처리를 해 두고 @Column 처리를 했었다. 400 번대로 안나오고 500으로만 나와서 굳이 작성할 필요가 없어서..
private void validateTitle(String title) {
if (title == null || title.isBlank()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "일정 제목은 필수입니다.");
}
if (title.length() > 30) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "일정 제목은 30자 이내로 입력해주세요.");
}
}
private void validateContent(String content) {
if (content == null || content.isBlank()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "일정 내용은 필수입니다.");
}
if (content.length() > 200) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "일정 내용은 200자 이내로 입력해주세요.");
}
}
private void validatePassword(String password) {
if (password == null || password.isBlank()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "비밀번호는 필수입니다.");
}
}
private void validateNickname(String nickName) {
if (nickName == null || nickName.isBlank()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "작성자명은 필수입니다.");
}
}
그러다가 ResponseStatusException 이라고 터미널에서는 안나오지만 포스트맨에서 Response 해주는 코드를 발견 해서 적용 시켰더니

400번대 오류가 잘 나오는 모습이다.
content, createAt 댓글 중에 이런게 뒤죽박죽 나온다...

public class GetCommentResponse {
private final Long id;
private final String content;
private final String nickName;
private final LocalDateTime createdAt;
private final LocalDateTime updatedAt;
public GetCommentResponse(Comment comment) {
this.id = comment.getId();
this.content = comment.getContent();
this.nickName = comment.getNickName();
this.createdAt = comment.getCreatedAt();
this.updatedAt = comment.getModifiedAt();
}
}
public class GetCommentResponse {
private final Long id;
private final String content;
private final String nickName;
private final LocalDateTime createdAt;
private final LocalDateTime updatedAt;
public GetCommentResponse(Long id, String content, String nickName, LocalDateTime createdAt, LocalDateTime updatedAt) {
this.id = id;
this.content = content;
this.nickName = nickName;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}
@Transactional(readOnly = true)
public GetScheduleCommentResponse findScheduleComment(Long scheduleId) {
Schedule schedule = scheduleRepository.findById(scheduleId).orElseThrow(
() -> new IllegalArgumentException("없는 스케줄입니다.")
);
List<Comment> comments = commentRepository.findAllByScheduleIdOrderByIdAsc(scheduleId);
List<GetCommentResponse> commentResponses = new ArrayList<>();
for (Comment comment : comments) {
commentResponses.add(new GetCommentResponse(comment));
}
return new GetScheduleCommentResponse(
schedule.getId(),
schedule.getTitle(),
schedule.getContent(),
schedule.getNickName(),
schedule.getCreatedAt(),
schedule.getModifiedAt(),
commentResponses
);
}
for (Comment comment : comments) {
commentResponses.add(new GetCommentResponse(comment));
}
for (Comment comment : comments) {
commentResponses.add(new GetCommentResponse(
comment.getId(),
comment.getContent(),
comment.getNickName(),
comment.getCreatedAt(),
comment.getModifiedAt()));
}
이렇게 바꿔 주니까

짜라란 잘 나오는 모습이다
+기찬님이 오늘 알려주신 내용
@JsonPropertyOrder({ "id", "title", "content", "createdAt" })
어노테이션을 활용해서 푸는 것도 가능 하다!!
@PathVariable에 익숙해서 2가지를 만들어서 진행 하려고 했는데 API는 1개만 만들어야 한다는 조건이 있어 고민 하다 튜터님께 질문 했다.
// 전체 조회
// 작성자명은 조회 조건으로 포함될 수도 있고, 포함되지 않을 수도 있습니다.
// ex) @GetMapping("/schedules/{nickName}")
// /schedules?nickName=김철수
// http://localhost:8080/schedules?nickName=최길중
@GetMapping("/schedules")
public ResponseEntity<List<GetScheduleResponse>> getSchedule(// String nickName) 원래 이거
@RequestParam(required = false) String nickName
){
// return ResponseEntity.status(HttpStatus.OK).body(scheduleService.findAll());
return ResponseEntity.ok(scheduleService.findAll(nickName));
}
@RequestParam을 이용해서 푸는 내용이였다.
그렇다면 service를 수정 해야 겠지?
원래 나는 그냥 GetMapping으로 전체 수정을 만들었다.
이전에 없었던 내용 추가
List<Schedule> schedules;
if (nickName == null ) {
schedules = scheduleRepository.findAll();
} else {
schedules = scheduleRepository.findAllByNickName(nickName); // repository에 추가 findAllByNickName
}


잘나오는 모습!!
3 Layer Architecture(Controller, Service, Repository)를 적절히 적용했는지 확인해 보고, 왜 이러한 구조가 필요한지 작성해 주세요.
답:
1) Controller
2) Service
3) Repository
@RequestParam, @PathVariable, @RequestBody가 각각 어떤 어노테이션인지, 어떤 특징을 갖고 있는지 작성해 주세요.
답:
https://velog.io/@choil8228/발제-트러블-슈팅Lv.2PathVariable-RequestParam-RequestBody
1) @PathVariable
2) @RequestParam
3) @RequestBody