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
34 lines (28 loc) · 1 KB
/
TodoClientWithRestClient.java
File metadata and controls
34 lines (28 loc) · 1 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
package cholog;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpStatusCode;
import org.springframework.web.client.RestClient;
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() {
return restClient.get()
.uri("/todos")
.retrieve()
.body(new ParameterizedTypeReference<List<Todo>>(){});
}
public Todo getTodoById(Long id) {
return restClient.get()
.uri("/todos/{todoId}", id)
.retrieve()
.onStatus(HttpStatusCode::is4xxClientError, (request, response) -> {
throw new TodoException.NotFound(id);
})
.body(Todo.class);
}
}