
org.sklsft.commons.rest.client.RestClient Maven / Gradle / Ivy
package org.sklsft.commons.rest.client;
import java.util.Map;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;
/**
* this class is responsible for calling rest services.
* It works as an adapter to the spring {@link RestTemplate}
* The rest server url should be given by a jndi variable through xml
* configuration.
*
* @author Nicolas Thibault
*
*/
public class RestClient {
/*
* dependencies to be injected with spring xml configuration
*/
private String restServerUrl;
private RestTemplate restTemplate;
/*
* setters for spring xml injection
*/
public void setRestServerUrl(String restServerUrl) {
this.restServerUrl = restServerUrl;
}
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
/**
* call a rest url with a GET method and maps the response to T
*/
public T getForObject(String relativeUrl, Class responseClass) {
return restTemplate
.getForObject(getFullUrl(relativeUrl), responseClass);
}
/**
* call a rest url with a GET method with url variables and maps the
* response to T
*/
public T getForObject(String relativeUrl, Class responseClass,
Map uriVariables) {
return restTemplate.getForObject(getFullUrl(relativeUrl),
responseClass, uriVariables);
}
/**
* call a rest url with a GET method and maps the response to the given
* parameterized type.
*/
public T getForObject(String relativeUrl,
ParameterizedTypeReference type) {
return restTemplate.exchange(getFullUrl(relativeUrl), HttpMethod.GET,
null, type).getBody();
}
/**
* call a rest url with a GET method with url variables and maps the
* response to the given parameterized type.
*/
public T getForObject(String relativeUrl,
ParameterizedTypeReference type, Map uriVariables) {
return restTemplate.exchange(getFullUrl(relativeUrl), HttpMethod.GET,
null, type, uriVariables).getBody();
}
/**
* call a rest url with a POST method and maps the response to T
*/
public T postForObject(String relativeUrl, Object form,
Class responseClass) {
return restTemplate.postForObject(getFullUrl(relativeUrl), form,
responseClass);
}
/**
* call a rest url with a POST method with url variables and maps the
* response to T
*/
public T postForObject(String relativeUrl, Object form,
Class responseClass, Map uriVariables) {
return restTemplate.postForObject(getFullUrl(relativeUrl), form,
responseClass, uriVariables);
}
/**
* call a rest url with a POST method and maps the response to the given
* parameterized type.
*/
public T postForObject(String relativeUrl, Object form,
ParameterizedTypeReference type) {
HttpEntity
© 2015 - 2025 Weber Informatics LLC | Privacy Policy