forked from cho-log/spring-learning-test
-
Notifications
You must be signed in to change notification settings - Fork 5
[spring-http-client-1] gib.son(손수환) 과제 제출합니다. #63
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
Open
swandevson
wants to merge
6
commits into
Japring-Study:gib/spring-http-client-1
Choose a base branch
from
swandevson:gib/spring-http-client-1
base: gib/spring-http-client-1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a448493
feat: restTemplate을 사용하여 요청을 보내고 결과를 Todo로 변환하여 반환
swandevson eb6dad1
feat: 존재하지 않는 id로 요청을 보낼 경우 TodoException.NotFound 예외를 던짐
swandevson 8de295e
feat: restClient의 get 메서드를 사용하여 요청을 보내고 결과를 Todo 리스트로 변환하여 반환
swandevson 5f0f02a
feat: restClient의 get 메서드를 사용하여 요청을 보내고 결과를 Todo로 변환하여 반환
swandevson e575662
feat: 존재하지 않는 id로 요청을 보낼 경우 TodoException.NotFound 예외를 던짐
swandevson ee52f7d
fix: Not Found 에러 핸들링을 4xx가 아닌 404에만 동작하도록 변경
swandevson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
40 changes: 24 additions & 16 deletions
40
spring-http-client-1/initial/src/main/java/cholog/TodoClientWithRestClient.java
This file contains hidden or 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,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<>() { | ||
| }); | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
26 changes: 17 additions & 9 deletions
26
spring-http-client-1/initial/src/main/java/cholog/TodoClientWithRestTemplate.java
This file contains hidden or 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,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) { | ||
|
Member
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. RestClientException 범위가 넓어서, 어떤 Exception들이 포함되는지 알아보는 것도 좋을 것 같습니다! |
||
| throw new TodoException.NotFound(id); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
저도 complete에 있는 거 보고 알았는데,
.body(Todo[].class)로 하고, List로 변환 하는 법도 있더라구요!