Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[week1] 1차 세미나 과제 #2

Merged
merged 3 commits into from
Oct 27, 2023
Merged

[week1] 1차 세미나 과제 #2

merged 3 commits into from
Oct 27, 2023

Conversation

ziiyouth
Copy link
Member

📌 관련 이슈

closed #1

✨ 과제 내용

1. 기본 과제

  • 세미나 이론 및 코드 복습

2. 심화 과제

  • 세미나에 있는 health check API에 ResponseEntity 대신 응답 객체 만들기

BaseResponse

  • 제너릭을 통한 BaseResponse로 다른 API의 응답 객체로 활용 가능하게 생성함. (데이터 반환하지 않을 때/데이터 반환할 때)
  • @JsonInclude 어노테이션을 이용해 data값이 null일 때는 응답에 포함되지 않게 설정함. (데이터 반환하지 않을 때)
  • access = AccessLevel.PRIVATE으로 클래스의 내부에서만 접근 가능하게 함.

HealthCheckController

  • 엔드포인트 /v6, /v7, /v8 생성
  • /v6 : BaseResponse 객체를 반환하며 ResponseEntity를 사용하지 않음. 응답이 단순하나 상태 코드 (헤더) 설정 불가.
  • /v7 : ResponseEntity 반환, .ok()로 응답 생성.
  • /v8 : ResponseEntity 반환, .status()와 .body()로 응답 생성. 특정 상태 코드와 응답 제어 가능.

3. 생각 과제

  • 키워드 정리 (PDF)
    • Java Generic
    • 자바 제어자와 접근 제어자에 대해 알아보기
    • 싱글톤(Singleton)
    • Spring의 각각의 의존성 주입 방법 특징 및 생성자 주입 방식의 장점
    • JAR, WAR의 차이점

🐥 총 정리

ResponseEntity를 가지고 어떤 형식의 응답을 취해야 할지에 대해 고민해보는 시간이었다.
ResponseEntity를 사용하지 않고 커스텀한 객체만 반환하면, 단순한 형태의 코드가 되지만 헤더의 HTTP Status Code를 설정하지 못한다는 문제점이 있다는 사실을 알게 되었다.

Copy link
Member

@unanchoi unanchoi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다~!

Comment on lines +6 to +30
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class BaseResponse<T> {

private final int code;

private final String status;

private final boolean success;

@JsonInclude(JsonInclude.Include.NON_NULL)
private T data;

// 데이터 반환하지 않을 때
public static BaseResponse success() {
return new BaseResponse<>(SuccessCode.OK.getStatusCode(), SuccessCode.OK.getStatus(), true);
}

// 데이터 반환할 때
public static <T> BaseResponse<T> success(T data) {
return new BaseResponse<>(SuccessCode.OK.getStatusCode(), SuccessCode.OK.getStatus(), true, data);
}

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

깔끔하게 설계하셨네요~!

Copy link
Member

@0lynny 0lynny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드보면서 새로운 것들 많이 배워갑니다~! 과제하느라 수고 많으셨습니당🥰

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p5;
.gitignore에 .DS_Store 추가해줘도 좋을 것 같아요~!

Comment on lines +57 to +61
@GetMapping("/v8")
public ResponseEntity<BaseResponse> healthCheckV8() {
return ResponseEntity.status(SuccessCode.OK.getStatusCode())
.body(BaseResponse.success());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p5;
이런 방법도 있군요! 배워갑니다~!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저랑 다른 방법들도 많이 배워가요!!


@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public enum SuccessCode {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p5;
enum으로 관리하는거 좋은 방법인 것 같아요! 공부해서 제 코드에도 적용해볼게요~!!

return ResponseEntity.ok(response);
}

@GetMapping("/v5")
Copy link

@Soyewon Soyewon Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P5 : 깔끔해요~ 많이 배워갑니다!!!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아이고 줄 선택이 잘못되었네요...ㅋㅋ큐ㅠㅠ 과제부분으로 봐주세요~

@ziiyouth ziiyouth merged commit a4c5652 into main Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[week1] 1차 세미나 과제
5 participants