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

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

Introduction

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

Prototype

<T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException;

Source Link

Document

Retrieve a representation by doing a GET on the URL .

Usage

From source file:com.oreilly.springdata.rest.client.OrderClient.java

public static void main(String[] args) {

    // Bootstrap RestOperations instance using Spring
    ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ClientConfiguration.class);
    context.registerShutdownHook();/*from  w w w  .  j  ava 2  s.  c o m*/
    RestOperations operations = context.getBean(RestOperations.class);

    // Access root resource
    ResourceSupport root = operations.getForEntity(ClientConfiguration.BASE_URL, Resource.class).getBody();
    Link orderLink = root.getLink(ClientConfiguration.ORDERS_REL);

    // Follow link to access orders
    Orders orders = operations.getForObject(orderLink.getHref(), Orders.class);

    for (Order order : orders) {

        // Follow link to access customer of the order
        Link link = order.getLink(ClientConfiguration.ORDER_CUSTOMER_REL);
        Customer customer = operations.getForObject(link.getHref(), CustomerClient.Customer.class);
        com.oreilly.springdata.rest.core.Customer domainObject = customer.getContent();
        System.out.println(
                "Order for customer: " + domainObject.getFirstname() + " " + domainObject.getLastname());
    }
}

From source file:org.trustedanalytics.metricsprovider.cloudadapter.RestOperationsHelpers.java

public static ResponseEntity<String> getForEntityWithToken(RestOperations restTemplate, String token,
        String url) {//from  w ww  .  j  av a2 s .c o m
    addAuthHeaderToTemplate(restTemplate, token);
    return restTemplate.getForEntity(url, String.class);
}

From source file:some.test.LaxTokenInfoResourceServerTokenServicesTest.java

@Test
public void invokeOAuthSecuredServiceWithLaxAuthorization() {
    RestOperations restOperations = buildClient("lax");

    restOperations.getForEntity(getBasePath() + "/secured/hello/bello", String.class);
}

From source file:some.test.LaxTokenInfoResourceServerTokenServicesTest.java

@Test
public void invokeOAuthSecuredServiceWithLaxAuthorizationAndTheValueSetToFalse() {
    RestOperations restOperations = buildClient("lax-with-false");

    restOperations.getForEntity(getBasePath() + "/secured/hello/bello", String.class);
}

From source file:some.test.AbstractTokenInfoResourceServerTokenServicesTest.java

@Test(expected = HttpClientErrorException.class)
public void invokeOAuthSecuredServiceWithInvalidToken() {
    RestOperations restOperations = buildClient("error");

    restOperations.getForEntity(getBasePath() + "/secured/hello/bello", String.class);
}

From source file:some.test.AbstractTokenInfoResourceServerTokenServicesTest.java

@Test(expected = HttpClientErrorException.class)
public void invokeOAuthSecuredServiceWithInvalidTokenNoUid() {
    RestOperations restOperations = buildClient("no-uid");

    restOperations.getForEntity(getBasePath() + "/secured/hello/bello", String.class);
}

From source file:some.test.AbstractTokenInfoResourceServerTokenServicesTest.java

@Test(expected = HttpClientErrorException.class)
public void invokeOAuthSecuredServiceWithInvalidTokenNoScope() {
    RestOperations restOperations = buildClient("no-scope");

    restOperations.getForEntity(getBasePath() + "/secured/hello/bello", String.class);
}

From source file:some.test.AbstractTokenInfoResourceServerTokenServicesTest.java

@Test(expected = HttpClientErrorException.class)
public void invokeOAuthSecuredServiceWithInvalidTokenEmptyUid() {
    RestOperations restOperations = buildClient("empty-uid");

    restOperations.getForEntity(getBasePath() + "/secured/hello/bello", String.class);
}

From source file:some.test.AbstractTokenInfoResourceServerTokenServicesTest.java

@Test
public void invokeOAuthSecuredService() {

    RestOperations restOperations = buildClient("123456789");

    ResponseEntity<String> responseEntity = restOperations.getForEntity(getBasePath() + "/secured/hello/bello",
            String.class);

    ///*from w w w  . ja  v  a  2s.  co m*/
    assertThat(responseEntity.getBody()).isEqualTo("hello bello");

}

From source file:org.cloudfoundry.identity.api.web.AppsIntegrationTests.java

/**
 * tests a happy-day flow of the native application profile.
 *//*from  w  w w .j  a  v a2 s  . c  o  m*/
@Test
public void testHappyDay() throws Exception {

    RestOperations restTemplate = serverRunning.createRestTemplate();
    ResponseEntity<String> response = restTemplate.getForEntity(serverRunning.getUrl("/api/apps"),
            String.class);
    // first make sure the resource is actually protected.
    assertNotSame(HttpStatus.OK, response.getStatusCode());
    HttpHeaders approvalHeaders = new HttpHeaders();
    OAuth2AccessToken accessToken = context.getAccessToken();
    approvalHeaders.set("Authorization", "bearer " + accessToken.getValue());
    Date oneMinuteAgo = new Date(System.currentTimeMillis() - 60000);
    Date expiresAt = new Date(System.currentTimeMillis() + 60000);
    ResponseEntity<Approval[]> approvals = serverRunning.getRestTemplate().exchange(
            serverRunning.getUrl("/uaa/approvals"), HttpMethod.PUT,
            new HttpEntity<Approval[]>((new Approval[] {
                    new Approval(testAccounts.getUserName(), "app", "cloud_controller.read", expiresAt,
                            ApprovalStatus.APPROVED, oneMinuteAgo),
                    new Approval(testAccounts.getUserName(), "app", "openid", expiresAt,
                            ApprovalStatus.APPROVED, oneMinuteAgo),
                    new Approval(testAccounts.getUserName(), "app", "password.write", expiresAt,
                            ApprovalStatus.APPROVED, oneMinuteAgo) }),
                    approvalHeaders),
            Approval[].class);
    assertEquals(HttpStatus.OK, approvals.getStatusCode());

    ResponseEntity<String> result = serverRunning.getForString("/api/apps");
    assertEquals(HttpStatus.OK, result.getStatusCode());
    String body = result.getBody();
    assertTrue("Wrong response: " + body, body.contains("dsyerapi.cloudfoundry.com"));

}