diff --git a/spring-http-client-1/initial/src/main/java/cholog/Todo.java b/spring-http-client-1/initial/src/main/java/cholog/Todo.java index 7f3644aa..abd003fb 100644 --- a/spring-http-client-1/initial/src/main/java/cholog/Todo.java +++ b/spring-http-client-1/initial/src/main/java/cholog/Todo.java @@ -2,9 +2,12 @@ public class Todo { - // TODO: Todo 객체가 가지는 필드들을 정의 + private Long userId; + private Long id; + private String title; + private Boolean completed; - public String getTitle() { - return null; - } + public String getTitle() { + return title; + } } diff --git a/spring-http-client-1/initial/src/main/java/cholog/TodoClientWithRestClient.java b/spring-http-client-1/initial/src/main/java/cholog/TodoClientWithRestClient.java index ab2b6f5c..e71a9cb3 100644 --- a/spring-http-client-1/initial/src/main/java/cholog/TodoClientWithRestClient.java +++ b/spring-http-client-1/initial/src/main/java/cholog/TodoClientWithRestClient.java @@ -1,25 +1,33 @@ package cholog; -import org.springframework.web.client.RestClient; - -import java.util.Collections; import java.util.List; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpStatus; +import org.springframework.web.client.RestClient; + public class TodoClientWithRestClient { - private final RestClient restClient; + private final RestClient restClient; - public TodoClientWithRestClient(RestClient restClient) { - this.restClient = restClient; - } + public TodoClientWithRestClient(RestClient restClient) { + this.restClient = restClient; + } - public List getTodos() { - // TODO: restClient의 get 메서드를 사용하여 요청을 보내고 결과를 Todo 리스트로 변환하여 반환 - return Collections.emptyList(); - } + public List getTodos() { + return restClient.get() + .uri("/todos") + .retrieve() + .body(new ParameterizedTypeReference<>() { + }); + } - public Todo getTodoById(Long id) { - // TODO: restClient의 get 메서드를 사용하여 요청을 보내고 결과를 Todo로 변환하여 반환 - // TODO: 존재하지 않는 id로 요청을 보낼 경우 TodoException.NotFound 예외를 던짐 - return new Todo(); - } + public Todo getTodoById(Long id) { + return restClient.get() + .uri("/todos/{id}", id) + .retrieve() + .onStatus(HttpStatus.NOT_FOUND::equals, (request, response) -> { + throw new TodoException.NotFound(id); + }) + .body(Todo.class); + } } diff --git a/spring-http-client-1/initial/src/main/java/cholog/TodoClientWithRestTemplate.java b/spring-http-client-1/initial/src/main/java/cholog/TodoClientWithRestTemplate.java index 0cc73f46..350907f6 100644 --- a/spring-http-client-1/initial/src/main/java/cholog/TodoClientWithRestTemplate.java +++ b/spring-http-client-1/initial/src/main/java/cholog/TodoClientWithRestTemplate.java @@ -1,17 +1,25 @@ package cholog; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; public class TodoClientWithRestTemplate { - private final RestTemplate restTemplate; + private static final String TODO_URL = "http://jsonplaceholder.typicode.com/todos"; - public TodoClientWithRestTemplate(RestTemplate restTemplate) { - this.restTemplate = restTemplate; - } + private final RestTemplate restTemplate; - public Todo getTodoById(Long id) { - // TODO: restTemplate을 사용하여 요청을 보내고 결과를 Todo로 변환하여 반환 - // TODO: 존재하지 않는 id로 요청을 보낼 경우 TodoException.NotFound 예외를 던짐 - return new Todo(); - } + public TodoClientWithRestTemplate(RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } + + public Todo getTodoById(Long id) { + try { + ResponseEntity todoResponseEntity = restTemplate.getForEntity(TODO_URL + "/" + id, Todo.class); + + return todoResponseEntity.getBody(); + } catch (RestClientException e) { + throw new TodoException.NotFound(id); + } + } }