forked from cho-log/spring-learning-test
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add RestClient learning test (cho-log#18)
* build: upgrade gradle version for 8.1.1 * feat: add RestClient learning test * feat: add RestClient learning test with initial status
- Loading branch information
Showing
25 changed files
with
478 additions
and
14 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip | ||
networkTimeout=10000 | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# 1. Spring의 HTTP 클라이언트 | ||
|
||
<br> | ||
|
||
스프링 프레임워크는 다양한 HTTP 클라이언트를 제공하여 애플리케이션이 외부 API와 통신할 수 있도록 합니다. | ||
스프링이 제공하는 HTTP 클라이언트에는 `RestClient`, `WebClient`, `RestTemplate` 이 있습니다. | ||
|
||
이 문서에서는 스프링 6.1 버전에 추가된 `RestClient` 에 대해 다룹니다. | ||
|
||
<br> | ||
|
||
### 참조 | ||
|
||
- [Spring - REST Clients](https://docs.spring.io/spring-framework/reference/integration/rest-clients.html) | ||
- [Spring blog - New in Spring 6.1: RestClient](https://spring.io/blog/2023/07/13/new-in-spring-6-1-restclient) | ||
|
||
<br> | ||
|
||
# 2. GET 요청 하기 | ||
|
||
<br> | ||
|
||
`RestClient` 의 `get()` 메서드를 사용해 GET 요청을 할 수 있습니다. | ||
|
||
<br> | ||
|
||
### 학습 테스트 | ||
- 테스트 메서드: `cholog.RestClientTest.testGetTodos` | ||
- 수행 방법 | ||
- `cholog.TodoRestClient.getTodos` 을 이용하여 학습 테스트를 성공시키세요. | ||
- `TodoRestClient` 의 restClient 를 사용하여 Todo 목록을 조회합니다. | ||
- Todo 목록 조회 url: http://jsonplaceholder.typicode.com/todos | ||
- 외부 API의 host는 `RestClientConfig` 에 설정되어 있습니다. | ||
|
||
<br> | ||
|
||
# 3. GET 요청의 응답 변환하기 | ||
|
||
<br> | ||
|
||
`RestClient` 의 `body()` 메서드를 사용해 응답을 원하는 POJO로 변환할 수 있습니다. | ||
|
||
<br> | ||
|
||
### 학습 테스트 | ||
- 테스트 메서드: `cholog.RestClientTest.testGetTodoWithId` | ||
- 수행 방법 | ||
- `cholog.TodoRestClient.getTodoById` 을 이용하여 학습 테스트를 성공시키세요. | ||
- `TodoRestClient` 의 restClient 를 사용하여 특정 id를 가진 Todo를 조회합니다. | ||
- Todo 조회 url: http://jsonplaceholder.typicode.com/todos/1 | ||
- 응답 Body로 받은 `userId`, `id`, `title`, `completed` 값이 잘 매핑되도록 `Todo` 를 수정합니다. | ||
|
||
<br> | ||
|
||
# 4. 예외 처리 | ||
|
||
<br> | ||
|
||
`RestClient` 의 `onStatus()` 메서드를 사용해 HTTP 응답 코드에 따라 예외처리를 할 수 있습니다. | ||
|
||
<br> | ||
|
||
### 학습 테스트 | ||
- 테스트 메서드: `cholog.RestClientTest.testGetTodoWithNonExistentId` | ||
- 수행 방법 | ||
- `cholog.TodoRestClient.getTodoById` 을 이용하여 학습 테스트를 성공시키세요. | ||
- 존재하지 않는 id에 대한 조회 요청을 했을 때, `TodoException.NotFound` 예외가 발생하도록 합니다. | ||
|
||
<br> | ||
|
||
# 5. 더 생각해보기 | ||
|
||
- 스프링이 제공하는 HTTP 클라이언트에는 각각 다른 성격과 사용 방법을 가지고 있습니다. | ||
- `RestClient` 외의 다른 클라이언트에 대한 학습 테스트를 작성해 보며 차이점을 느껴봅니다. | ||
- 각 클라이언트의 특징을 찾아보고 어떤 상황에 어떤 클라이언트를 택하는게 좋을지 생각해 봅니다. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
plugins { | ||
id 'org.springframework.boot' version '3.2.5' | ||
id 'io.spring.dependency-management' version '1.1.4' | ||
id 'java' | ||
} | ||
|
||
group = 'cholog' | ||
version = '0.0.1-SNAPSHOT' | ||
sourceCompatibility = '17' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation 'org.springframework.boot:spring-boot-starter-web' | ||
testImplementation 'org.springframework.boot:spring-boot-starter-test' | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rootProject.name = 'spring-http-client-1' |
15 changes: 15 additions & 0 deletions
15
spring-http-client-1/complete/src/main/java/cholog/RestClientConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package cholog; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestClient; | ||
|
||
@Configuration | ||
public class RestClientConfig { | ||
@Bean | ||
public TodoRestClient todoRestClient() { | ||
return new TodoRestClient( | ||
RestClient.builder().baseUrl("http://jsonplaceholder.typicode.com").build() | ||
); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
spring-http-client-1/complete/src/main/java/cholog/SpringClientApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cholog; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class SpringClientApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SpringClientApplication.class, args); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
spring-http-client-1/complete/src/main/java/cholog/Todo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cholog; | ||
|
||
public class Todo { | ||
private Long id; | ||
private Long userId; | ||
private String title; | ||
private boolean completed; | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public Long getUserId() { | ||
return userId; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public boolean isCompleted() { | ||
return completed; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
spring-http-client-1/complete/src/main/java/cholog/TodoException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cholog; | ||
|
||
public class TodoException extends RuntimeException { | ||
public TodoException(String message) { | ||
super(message); | ||
} | ||
|
||
public static class NotFound extends TodoException { | ||
public NotFound(Long id) { | ||
super("Todo not found with id: " + id); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
spring-http-client-1/complete/src/main/java/cholog/TodoRestClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cholog; | ||
|
||
import org.springframework.web.client.RestClient; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class TodoRestClient { | ||
private final RestClient restClient; | ||
|
||
public TodoRestClient(RestClient restClient) { | ||
this.restClient = restClient; | ||
} | ||
|
||
public List<Todo> getTodos() { | ||
Todo[] todoBody = restClient.get() | ||
.uri("/todos") | ||
.retrieve() | ||
.body(Todo[].class); | ||
|
||
return Arrays.asList(todoBody); | ||
} | ||
|
||
public Todo getTodoById(Long id) { | ||
return restClient.get() | ||
.uri("/todos/{id}", id) | ||
.retrieve() | ||
.onStatus(status -> status.value() == 404, (req, res) -> { | ||
throw new TodoException.NotFound(id); | ||
}) | ||
.body(Todo.class); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
spring-http-client-1/complete/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
37 changes: 37 additions & 0 deletions
37
spring-http-client-1/complete/src/test/java/cholog/RestClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cholog; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
@SpringBootTest | ||
public class RestClientTest { | ||
|
||
@Autowired | ||
private TodoRestClient todoRestClient; | ||
|
||
@Test | ||
public void testGetTodos() { | ||
List<Todo> todos = todoRestClient.getTodos(); | ||
assertThat(todos).isNotEmpty(); | ||
} | ||
|
||
@Test | ||
public void testGetTodoWithId() { | ||
Todo todo = todoRestClient.getTodoById(1L); | ||
assertThat(todo.getTitle()).isNotEmpty(); | ||
} | ||
|
||
@Test | ||
public void testGetTodoWithNonExistentId() { | ||
Long nonExistentId = 9999L; | ||
|
||
assertThatThrownBy(() -> todoRestClient.getTodoById(nonExistentId)) | ||
.isInstanceOf(TodoException.NotFound.class); | ||
} | ||
} |
Oops, something went wrong.