Example usage for org.springframework.web.client RestTemplate RestTemplate

List of usage examples for org.springframework.web.client RestTemplate RestTemplate

Introduction

In this page you can find the example usage for org.springframework.web.client RestTemplate RestTemplate.

Prototype

public RestTemplate() 

Source Link

Document

Create a new instance of the RestTemplate using default settings.

Usage

From source file:ymanv.forex.ForexApplication.java

@Bean
public RestTemplate template() {
    return new RestTemplate();
}

From source file:org.zalando.github.spring.AbstractTemplateTest.java

@Before
public void setup() {
    restTemplate = new RestTemplate();
    // this.gitHub = new GitHubTemplate("ACCESS_TOKEN");
    this.mockServer = MockRestServiceServer.createServer(restTemplate);

    this.responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.APPLICATION_JSON);

    // this.unauthorizedGitHub = new GitHubTemplate();

    // Create a mock server just to avoid hitting real GitHub if something
    // gets past the authorization check.
    // MockRestServiceServer.createServer(unauthorizedGitHub.getRestTemplate());
}

From source file:com.github.cjm.service.BaseService.java

public ResourceCollection load(String resource, Class clazz, int page) {

    StringBuilder uri = new StringBuilder(MOVIEDB_URL);
    uri.append(resource);/*from  w  ww .  ja  v a 2 s.  c o  m*/
    uri.append("?api_key=").append(apiKey);
    if (page > 1) {
        uri.append("&page=").append(page);
    }

    RestTemplate restTemplate = new RestTemplate();
    ResourceCollection<T> resourceCollection = new ResourceCollection<>();
    String data = null;
    try {
        data = restTemplate.getForObject(uri.toString(), String.class);
    } catch (HttpClientErrorException e) {
        log.warn(e.getMessage());
    }
    if (data != null) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            TypeFactory t = TypeFactory.defaultInstance();
            resourceCollection = mapper.readValue(data, t.constructType(clazz));
        } catch (IOException ex) {
            log.error("ObjectMapper exception: ", ex);
        }
    }
    return resourceCollection;
}

From source file:org.openwms.tms.routing.ModuleConfig.java

public @LoadBalanced @Bean RestTemplate restTemplate() {
    return new RestTemplate();
}

From source file:com.xpanxion.restclientexample.Application.java

public User getUser(long userId) {
    RestTemplate rt = new RestTemplate();
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJacksonHttpMessageConverter());
    rt.setMessageConverters(converters);
    return rt.getForObject("http://localhost:8080/UserProjectHibernate/rest/user/" + userId, User.class);
}

From source file:net.longfalcon.newsj.service.RegexService.java

public void checkRegexesUptoDate(String latestRegexUrl, int latestRegexRevision) {
    RestTemplate restTemplate = new RestTemplate();

    String responseString = restTemplate.getForObject(latestRegexUrl, String.class);

    Pattern pattern = Pattern.compile("\\/\\*\\$Rev: (\\d{3,4})", Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(responseString);

    if (matcher.find()) {
        int revision = Integer.parseInt(matcher.group(1));
        if (revision > latestRegexRevision) {
            _log.info(/*  ww  w.jav  a2 s.c o  m*/
                    "URL " + latestRegexUrl + " reports new regexes. Run the following SQL at your own risk:");
            _log.info(responseString);
        }
    }
}

From source file:io.spring.ScriptTemplateControllerIntegrationTests.java

@Test
public void home() {
    RestTemplate restTemplate = new RestTemplate();
    String result = restTemplate.getForObject("http://localhost:" + port, String.class);
    assertTrue(result.contains("<li>author1 content1</li>"));
}

From source file:org.messic.android.util.RestJSONClient.java

/**
 * Rest POST petition to the server at the url param, sending all the post parameters defiend at formData. This post
 * return an object (json marshalling) of class defined at clazz parameter. You should register a
 * {@link RestListener} in order to obtain the returned object, this is because the petition is done in an async
 * process.//from   w w  w  .  ja  v  a  2s . co m
 * 
 * @param url {@link string} URL to attack
 * @param formData {@link MultiValueMap}<?,?/> map of parameters to send
 * @param clazz Class<T/> class that you will marshall to a json object
 * @param rl {@link RestListener} listener to push the object returned
 */
public static <T> void post(final String url, MultiValueMap<?, ?> formData, final Class<T> clazz,
        final RestListener<T> rl) {
    final RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    // Sending multipart/form-data
    requestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    // Populate the MultiValueMap being serialized and headers in an HttpEntity object to use for the request
    final HttpEntity<MultiValueMap<?, ?>> requestEntity = new HttpEntity<MultiValueMap<?, ?>>(formData,
            requestHeaders);

    AsyncTask<Void, Void, Void> at = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, clazz);
                rl.response(response.getBody());
            } catch (Exception e) {
                rl.fail(e);
            }
            return null;
        }

    };

    at.execute();
}

From source file:org.trustedanalytics.scoringengine.ATKScoringEngine.java

public Boolean score(float[] data) {
    String commaSeparatedNumbers = convertToCommaSeparated(data);
    String url = getUrl() + commaSeparatedNumbers;
    Float result;/*ww  w  .  j  a  v a2  s . c o  m*/
    try {
        RestTemplate template = new RestTemplate();
        ResponseEntity<String> response = template.postForEntity(url, null, String.class);
        String body = response.getBody();
        result = Float.parseFloat(body);
        LOG.debug("Score from scoring engine: {}", result);
    } catch (Exception ex) {
        LOG.warn("problem with getting scoring result! " + ex.getMessage(), ex);
        result = -100f;
    }

    return result.compareTo(1.0f) == 0;
}

From source file:org.mimacom.sample.integration.patterns.user.service.integration.SimpleSearchServiceIntegration.java

public SimpleSearchServiceIntegration(String searchServiceUrl) {
    this.searchServiceUrl = searchServiceUrl;
    this.restTemplate = new RestTemplate();
}