forked from cho-log/spring-learning-test
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTodoClientWithRestClient.java
More file actions
38 lines (29 loc) · 1.01 KB
/
TodoClientWithRestClient.java
File metadata and controls
38 lines (29 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package cholog;
import org.springframework.http.HttpStatusCode;
import org.springframework.web.client.RestClient;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class TodoClientWithRestClient {
private final RestClient restClient;
public TodoClientWithRestClient(RestClient restClient) {
this.restClient = restClient;
}
public List<Todo> getTodos() {
Todo[] result = restClient.get()
.uri("http://jsonplaceholder.typicode.com/todos")
.retrieve()
.body(Todo[].class);
return Arrays.asList(result);
}
public Todo getTodoById(Long id) {
Todo result = restClient.get()
.uri("http://jsonplaceholder.typicode.com/todos/{id}", id)
.retrieve()
.onStatus(status -> status.value() == 404, (req, res) -> {
throw new TodoException.NotFound(id);
})
.body(Todo.class);
return result;
}
}