-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathUserRestRepository.java
More file actions
42 lines (32 loc) · 1.13 KB
/
Copy pathUserRestRepository.java
File metadata and controls
42 lines (32 loc) · 1.13 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
39
40
41
42
package scratches.boot.batchrest.user;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.util.List;
import static org.springframework.http.HttpMethod.GET;
/**
* @author Rashidi Zin
*/
@Repository
public class UserRestRepository {
private final RestTemplate restTemplate;
public UserRestRepository(RestTemplateBuilder builder) {
this.restTemplate = builder.build();
}
@Nullable
public User findByUsername(String username) {
return findAll().stream()
.filter(user -> username.equalsIgnoreCase(user.getUsername()))
.findFirst()
.orElse(null);
}
private List<User> findAll() {
var uri = URI.create("https://jsonplaceholder.typicode.com/users");
return restTemplate
.exchange(uri, GET, null, new ParameterizedTypeReference<List<User>>() {})
.getBody();
}
}