-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestTemplateUtils.java
More file actions
42 lines (33 loc) · 1.26 KB
/
RestTemplateUtils.java
File metadata and controls
42 lines (33 loc) · 1.26 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
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import java.io.IOException;
import java.net.URI;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;
/**
* @author atul94
*/
public class RestTemplateUtils<T, U> {
private RestTemplateUtils() {
/**
* This is a utility class
*/
}
private static <U> String getResponseAsString(RestTemplate restTemplate, HttpEntity<U> entity,
URI uri) {
return restTemplate.exchange(uri, HttpMethod.GET, entity, String.class).getBody();
}
public static <T> T mapJsonResponseToObject(RestTemplate restTemplate, URI uri, Class<T> type) {
return restTemplate.getForObject(uri, type);
}
public static <T, U> T mapSmallJsonResponseToObject(RestTemplate restTemplate, Gson gson, URI uri,
HttpEntity<U> entity, Class<T> type) {
return gson.fromJson(getResponseAsString(restTemplate, entity, uri), type);
}
public static <T, U> T mapBigJsonResponseToObject(RestTemplate restTemplate,
ObjectMapper objectMapper, URI uri,
HttpEntity<U> entity, Class<T> type) throws IOException {
return objectMapper.readValue(getResponseAsString(restTemplate, entity, uri), type);
}
}