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

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

Introduction

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

Prototype

@Override
    public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException 

Source Link

Usage

From source file:com.manh.cp.fw.swagger.SwaggerConfigTest.java

@Test
public void simple_controller() {
    RestTemplate restTemplate = new RestTemplate();

    ResponseEntity<String> jsonResponseEntity = restTemplate
            .getForEntity("http://localhost:" + localPort + "/v2/api-docs", String.class);

    assertThat(jsonResponseEntity.getStatusCode(), equalTo(HttpStatus.OK));

    String json = jsonResponseEntity.getBody();

    assertThat(json, isJson());/*from w  w  w  . j a va2 s  . c  om*/
    assertThat(json, hasJsonPath("$.paths.*", hasSize(1)));

}

From source file:me.j360.boot.standard.test.SessionRedisApplicationTests.java

@Test
public void sessionExpiry() throws Exception {

    String port = null;/* ww w  . ja  va 2 s. co  m*/

    try {
        ConfigurableApplicationContext context = new SpringApplicationBuilder().sources(J360Configuration.class)
                .properties("server.port:0").initializers(new ServerPortInfoApplicationContextInitializer())
                .run();
        port = context.getEnvironment().getProperty("local.server.port");
    } catch (RuntimeException ex) {
        if (!redisServerRunning(ex)) {
            return;
        }
    }

    URI uri = URI.create("http://localhost:" + port + "/");
    RestTemplate restTemplate = new RestTemplate();

    ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);
    String uuid1 = response.getBody();
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("Cookie", response.getHeaders().getFirst("Set-Cookie"));

    RequestEntity<Void> request = new RequestEntity<Void>(requestHeaders, HttpMethod.GET, uri);

    String uuid2 = restTemplate.exchange(request, String.class).getBody();
    assertThat(uuid1, is(equalTo(uuid2)));

    Thread.sleep(5000);

    String uuid3 = restTemplate.exchange(request, String.class).getBody();
    assertThat(uuid2, is(not(equalTo(uuid3))));
}

From source file:sample.tomcat.X509ApplicationTests.java

@Test(expected = ResourceAccessException.class)
public void testUnauthenticatedHello() throws Exception {
    RestTemplate template = new TestRestTemplate();
    ResponseEntity<String> httpsEntity = template.getForEntity("https://localhost:" + this.port + "/hello",
            String.class);
    assertEquals(HttpStatus.OK, httpsEntity.getStatusCode());
    assertEquals("hello", httpsEntity.getBody());
}

From source file:sample.tomcat.SslApplicationTests.java

@Test(expected = ResourceAccessException.class)
public void testUnauthenticatedHello() throws Exception {
    RestTemplate template = new RestTemplate();
    ResponseEntity<String> httpsEntity = template.getForEntity("https://localhost:" + this.port + "/hello",
            String.class);
    assertEquals(HttpStatus.OK, httpsEntity.getStatusCode());
    assertEquals("hello", httpsEntity.getBody());
}

From source file:org.cloudfoundry.identity.uaa.integration.util.IntegrationTestUtils.java

@SuppressWarnings("rawtypes")
public static Map findAllGroups(RestTemplate client, String url) {
    ResponseEntity<Map> response = client.getForEntity(url + "/Groups", Map.class);

    @SuppressWarnings("rawtypes")
    Map results = response.getBody();
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertTrue("There should be more than zero groups", (Integer) results.get("totalResults") > 0);
    return results;
}

From source file:com.kurento.kmf.repository.test.OneRecordingServerTest.java

protected void downloadFromURL(String urlToDownload, File downloadedFile) throws Exception {

    if (!downloadedFile.exists()) {
        downloadedFile.createNewFile();//from w ww  . j  a v  a  2 s .c om
    }

    log.info(urlToDownload);

    RestTemplate client = new RestTemplate();
    ResponseEntity<byte[]> response = client.getForEntity(urlToDownload, byte[].class);

    assertEquals(HttpStatus.OK, response.getStatusCode());

    FileOutputStream os = new FileOutputStream(downloadedFile);
    os.write(response.getBody());
    os.close();
}

From source file:example.pki.SslPkiTests.java

/**
 * The configured client truststore contains just the Root CA certificate. The
 * intermediate and server certificates are provided by the server SSL configuration.
 *///from   w  w w.  j  a v  a  2s .  c  o  m
@Test
public void shouldWorkWithGeneratedSslCertificate() {

    RestTemplate restTemplate = new RestTemplate(configuredWrapper.getClientHttpRequestFactory());

    ResponseEntity<String> response = restTemplate.getForEntity("https://localhost:" + port, String.class);

    assertThat(response.getStatusCodeValue()).isEqualTo(200);
    assertThat(response.getBody()).isEqualTo("Hello, World");
}

From source file:org.zaizi.SensefyResourceApplicationTests.java

@Test
public void testRootPathAnauthorized() {
    RestTemplate template = new TestRestTemplate();
    ResponseEntity<String> response = template.getForEntity(baseTestServerUrl(), String.class);
    Assert.assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
}

From source file:projekat.rest_client.Test.java

public static void main2(String[] args)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {

    String uri = "https://localhost:8443/secure/rest/places";
    RestTemplateFactory factory = new RestTemplateFactory();
    factory.afterPropertiesSet();/*from   w w w. ja  v a2 s .  co m*/
    RestTemplate restTemplate = factory.getObject();
    //        restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<T>(createHeaders("marko", "marko")), List.class);
    HttpClient httpClient = factory.getAuth().getHttpClient();
    TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
        @Override
        public boolean isTrusted(X509Certificate[] certificate, String authType) {
            return true;
        }
    };
    SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, ALLOW_ALL_HOSTNAME_VERIFIER);
    httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 8443, sf));
    ResponseEntity<List> exchange = restTemplate.exchange(uri, HttpMethod.POST,
            new HttpEntity<>(createHeaders("marko", "marko")), List.class);

    List body = exchange.getBody();
    for (Object object : body) {
        System.out.println("Mesto: " + object);
    }

    System.out.println("******************");
    ResponseEntity<List> forEntity = restTemplate.getForEntity(uri, List.class);
    List body1 = forEntity.getBody();
    for (Object object : body1) {
        System.out.println("Mesto: " + object);
    }

}

From source file:org.apache.knox.solr.ui.SearchController.java

/**
 * Do a solr search/*  www.  j av  a  2s  . co m*/
 * 
 * @param solrRequest
 *            the Solr request
 * @return the HTML response
 */
@RequestMapping("/search")
public @ResponseBody String search(@RequestBody SolrRequest solrRequest) {
    logger.debug("Received Solr Request: {}", solrRequest);
    final String solrUrl = buildSolrUrl(solrRequest);
    logger.debug("Executing with URL: {}", solrUrl);
    final RestTemplate restTemplate = new RestTemplate();

    final ResponseEntity<String> solrResult = restTemplate.getForEntity(solrUrl, String.class);
    logger.debug("Solr Result: {}", solrResult);
    if (HttpStatus.OK.equals(solrResult.getStatusCode())) {
        return solrResult.getBody();
    } else {
        logger.error("Error getting Solr Result - http status: {}", solrResult.getStatusCodeValue());
        return "";
    }
}