-
Notifications
You must be signed in to change notification settings - Fork 170
[Spring MVC] 김하늘 미션제출합니다 #508
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
base: kimsky247-coder
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # 방탈출 예약 시스템 | ||
|
|
||
| ## 주요 기능 | ||
| ### 1단계: 홈화면 | ||
| - localhost:8080 요청 시 아래 화면과 같이 어드민 메인 페이지가 응답할 수 있도록 구현한다. | ||
|
|
||
| ### 2단계: 예약 조회 | ||
| - /reservation 요청 시 예약 관리 페이지가 응답할 수 있도록 구현한다. | ||
| - 예약 관리 페이지 로드 시 호출되는 예약 목록 조회 API를 구현한다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip | ||
| networkTimeout=10000 | ||
| validateDistributionUrl=true | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package roomescape.controller; | ||
|
|
||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
|
|
||
| @Controller | ||
| public class HomeController { | ||
| @GetMapping("/") | ||
| public String home() { | ||
| return "home"; | ||
| } | ||
|
Comment on lines
+8
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요 메서드의 작동 원리도 한번 설명해주세요!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package roomescape.controller; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import roomescape.domain.Reservation; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.LocalTime; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| @Controller | ||
| public class ReservationController { | ||
| private final List<Reservation> reservations = new ArrayList<>(); | ||
| private AtomicLong index = new AtomicLong(1); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 직접 id값을 하드코딩 해 넣지 않고, 자동으로 증가시켜주는 것 이 정말 좋네요!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 만약 |
||
|
|
||
| public ReservationController() { | ||
| reservations.add(new Reservation(index.getAndIncrement(), "브라운", LocalDate.parse("2023-01-01"), LocalTime.parse("10:00"))); | ||
| reservations.add(new Reservation(index.getAndIncrement(), "브라운", LocalDate.parse("2023-01-02"), LocalTime.parse("11:00"))); | ||
| reservations.add(new Reservation(index.getAndIncrement(), "브라운", LocalDate.parse("2023-01-03"), LocalTime.parse("12:00"))); | ||
| } | ||
|
|
||
| @GetMapping("/reservation") | ||
| public String reservation() { | ||
| return "reservation"; | ||
| } | ||
|
Comment on lines
+25
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 메서드의 작동 원리를 한번 설명해주시겠어요?!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| @GetMapping("/reservations") | ||
| public ResponseEntity<List<Reservation>> getReservations() { | ||
| return ResponseEntity.ok().body(reservations); | ||
| } | ||
|
Comment on lines
+30
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
이 메서드는 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 위에 각 메서드의 작동 흐름을 설명해주셨을 때 작성해주셨듯이, 하지만 해당 메서드의(getReservations) 경우에는 view 템플릿을 반환하는 것 이 아닌 json을 직접 반환하는 메서드예요. 이때 사용할 수 있는 어노테이션이
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
두 어노테이션을 합친 것이 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 잘 정리해주셨어요! |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package roomescape.domain; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonFormat; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.LocalTime; | ||
|
|
||
| public class Reservation { | ||
| private Long id; | ||
| private String name; | ||
| private LocalDate date; | ||
|
|
||
| @JsonFormat(pattern = "HH:mm") | ||
| private LocalTime time; | ||
|
Comment on lines
+13
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 응답 JSON의 시간 포맷을 지정해주셨네요!👍
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| public Reservation(Long id, String name, LocalDate date, LocalTime time) { | ||
| this.id = id; | ||
| this.name = name; | ||
| this.date = date; | ||
| this.time = time; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public LocalDate getDate() { | ||
| return date; | ||
| } | ||
|
|
||
| public LocalTime getTime() { | ||
| return time; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,38 @@ | ||
| package roomescape; | ||
|
|
||
| import io.restassured.RestAssured; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.test.annotation.DirtiesContext; | ||
|
|
||
| import static org.hamcrest.core.Is.is; | ||
|
|
||
| @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) | ||
| @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) | ||
| public class MissionStepTest { | ||
|
|
||
| @Test | ||
| @DisplayName("메인 페이지가 정상적으로 응답한다.") | ||
| void 일단계() { | ||
| RestAssured.given().log().all() | ||
| .when().get("/") | ||
| .then().log().all() | ||
| .statusCode(200); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("예약 페이지 및 목록 조회 API가 정상적으로 응답한다.") | ||
| void 이단계() { | ||
| RestAssured.given().log().all() | ||
| .when().get("/reservation") | ||
| .then().log().all() | ||
| .statusCode(200); | ||
|
|
||
| RestAssured.given().log().all() | ||
| .when().get("/reservations") | ||
| .then().log().all() | ||
| .statusCode(200) | ||
| .body("size()", is(3)); // 아직 생성 요청이 없으니 Controller에서 임의로 넣어준 Reservation 갯수 만큼 검증하거나 0개임을 확인하세요. | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spring-boot-starter와spring-boot-starter-web의 차이에대해 설명해주세요!추가로
implementation과testImplementation에 대해서도 설명 부탁드립니다~There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spring Boot에서는
spring-boot-starter라는 편리한 의존성 조합을 제공합니다.프로젝트에 설정해야하는 다수의 의존성들을
starter가 이미 포함하고 있기 때문에starter에 대한 의존성 추가만으로도 프로젝트를 시작하거나 새로운 기능을 추가할 수 있습니다.하지만 웹 서버가 포함되어 있지 않아 주로 웹 기능이 없는 단순한 콘솔 애플리케이션이나 백그라운드 작업을 만들 때 사용합니다.
spring-boot-starter-web은spring-boot-starter의 모든 기능을 포함하고, 추가로 웹 애플리케이션 개발에 필요한 기능을 포함합니다. 내장 Tomcat 웹 서버, Spring MVC 프레임워크, JSON 변환기가 포함되어있어@Controller를 사용하고 웹 서버를 띄우려면spring-boot-starter-web이 반드시 필요합니다.implementation과testImplementation은 Gradle이 의존성을 관리하는 범위를 지정합니다implementation는 src/main/java에 있는 메인 애플리케이션 코드를 컴파일하고 실행할 때 필요한 라이브러리입니다. 이 의존성은 최종적으로 빌드되는 실행 파일에 포함됩니다.testImplementation는 src/test/java에 있는 테스트 코드를 컴파일하고 실행할 때만 필요한 라이브러리입니다. 테스트를 실행할 때는 사용되지만, 최종 실행 파일에는 포함되지 않습니다.