Skip to content

Instantly share code, notes, and snippets.

@woemler
Last active December 30, 2018 17:11
Show Gist options
  • Save woemler/5b624da34216d92f4fa9 to your computer and use it in GitHub Desktop.
Save woemler/5b624da34216d92f4fa9 to your computer and use it in GitHub Desktop.
Simple wrapper for Spring RestTemplate requests and object mapping.
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.*;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.List;
/**
* @author woemler
*/
public class RestUtils {
public static boolean isError(HttpStatus status) {
HttpStatus.Series series = status.series();
return !status.equals(HttpStatus.NOT_FOUND) && (HttpStatus.Series.SERVER_ERROR.equals(series) || HttpStatus.Series.CLIENT_ERROR
.equals(series));
}
public static HttpEntity<String> createRequest(){
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
return new HttpEntity<String>(headers);
}
public static RestTemplate getRestTemplate(){
return new RestTemplate();
}
public static ObjectMapper getObjectMapper(){
return new ObjectMapper();
}
public static <T> T fetchSingle(String url, Class<T> model, String id){
HttpEntity<String> request = createRequest();
ResponseEntity<String> response = getRestTemplate().exchange(
url,
HttpMethod.GET,
request,
String.class,
id
);
try {
if (RestUtils.isError(response.getStatusCode())) {
RestError restError = getObjectMapper().readValue(response.getBody(), RestError.class);
throw new RestClientException("[" + restError.getCode() + "] " + restError.getMessage());
} else {
return getObjectMapper().readValue(response.getBody(), model);
}
} catch (IOException e){
throw new RuntimeException(e);
}
}
public static <T> T fetchSingle(String url, Class<T> model, Object[] parameters){
HttpEntity<String> request = createRequest();
ResponseEntity<String> response = getRestTemplate().exchange(
url,
HttpMethod.GET,
request,
String.class,
parameters
);
try {
if (RestUtils.isError(response.getStatusCode())) {
RestError restError = getObjectMapper().readValue(response.getBody(), RestError.class);
throw new RestClientException("[" + restError.getCode() + "] " + restError.getMessage());
} else {
return getObjectMapper().readValue(response.getBody(), model);
}
} catch (IOException e){
throw new RuntimeException(e);
}
}
public static <T> List<T> fetchMultiple(String url, Class<T> model, Object[] parameters){
HttpEntity<String> request = createRequest();
ResponseEntity<String> response = getRestTemplate().exchange(
url,
HttpMethod.GET,
request,
String.class,
parameters
);
try {
if (RestUtils.isError(response.getStatusCode())) {
RestError restError = getObjectMapper().readValue(response.getBody(), RestError.class);
throw new RestClientException("[" + restError.getCode() + "] " + restError.getMessage());
} else {
return getObjectMapper().readValue(response.getBody(), getObjectMapper().getTypeFactory().constructCollectionType(List.class, model));
}
} catch (IOException e){
throw new RuntimeException(e);
}
}
public static <T> List<T> fetchMultiple(String url, Class<T> model){
return fetchMultiple(url, model, new Object[]{});
}
public static class RestError {
private HttpStatus status;
private Integer code;
private String message;
private String developerMessage;
private String moreInfoUrl;
public RestError() { }
public RestError(HttpStatus status, Integer code, String message, String developerMessage,
String moreInfoUrl) {
if (status == null){
throw new NullPointerException("HttpStatus argument cannot be null.");
}
this.status = status;
this.code = code;
this.message = message;
this.developerMessage = developerMessage;
this.moreInfoUrl = moreInfoUrl;
}
public HttpStatus getStatus() {
return status;
}
public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
public String getDeveloperMessage() {
return developerMessage;
}
public String getMoreInfoUrl() {
return moreInfoUrl;
}
}
}
@robsonlira
Copy link

Hello! I'm trying to consume an API, and found its wrapper class, the api I'm consuming has a complex JSON
https://bittrex.com/api/v1.1/public/getmarkets
so I have a model with generics ApiResult that has the attributes success, message and T result how to use your wrapper and for this Pojo?
ApiResult <List > apiResult

Robson C

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment