Skip to content
11 changes: 7 additions & 4 deletions spring-http-client-1/initial/src/main/java/cholog/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Todo> getTodos() {
// TODO: restClient의 get 메서드를 사용하여 요청을 보내고 결과를 Todo 리스트로 변환하여 반환
return Collections.emptyList();
}
public List<Todo> getTodos() {
return restClient.get()
.uri("/todos")
.retrieve()
.body(new ParameterizedTypeReference<>() {
});
Comment on lines +20 to +21
Copy link
Member

@yohanii yohanii Aug 28, 2024

Choose a reason for hiding this comment

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

저도 complete에 있는 거 보고 알았는데,
.body(Todo[].class)로 하고, List로 변환 하는 법도 있더라구요!

}

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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Todo> todoResponseEntity = restTemplate.getForEntity(TODO_URL + "/" + id, Todo.class);

return todoResponseEntity.getBody();
} catch (RestClientException e) {
Copy link
Member

Choose a reason for hiding this comment

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

RestClientException 범위가 넓어서, 어떤 Exception들이 포함되는지 알아보는 것도 좋을 것 같습니다!

throw new TodoException.NotFound(id);
}
}
}