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

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

Introduction

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

Prototype

@Nullable
<T> T getForObject(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.ProductsClient.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 av a 2  s  .  c o m
    RestOperations operations = context.getBean(RestOperations.class);

    // Access root resource
    ResourceSupport root = operations.getForObject(ClientConfiguration.BASE_URL, Resource.class);
    Link productLink = root.getLink(ClientConfiguration.PRODUCTS_REL);

    // Follow link to access products
    Products products = operations.getForObject(productLink.getHref(), Products.class);
    System.out.println(products.getContent().iterator().next());
}

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

public static void main(String[] args) {

    // Setup RestTemplate though Spring
    ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ClientConfiguration.class);
    context.registerShutdownHook();// w w w  . java2  s. co m
    RestOperations restOperations = context.getBean(RestOperations.class);

    // Access root resource
    ResourceSupport result = restOperations.getForObject(ClientConfiguration.BASE_URL, Resource.class);

    Link link = result.getLink(ClientConfiguration.CUSTOMERS_REL);
    System.out.println("Following: " + link.getHref());

    // Follow link relation for customers to access those
    Customers customers = restOperations.getForObject(link.getHref(), Customers.class);

    for (Customer dto : customers) {
        com.oreilly.springdata.rest.core.Customer customer = dto.getContent();
        System.out.println(customer.getFirstname() + " " + customer.getLastname());
    }
}

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();/*  w w  w . ja  v a2  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:com.aktios.appthree.server.service.OAuthAuthenticationService.java

public String loginApp() throws JSONException {
    RestOperations rest = new RestTemplate();
    String resAuth = rest.getForObject(oauthServerBaseURL + "/oauth/token?client_id=" + appToken
            + "&client_secret=" + appPassword + "&grant_type=client_credentials", String.class);
    System.out.println(resAuth);// w ww  .  jav  a 2 s  .  c  o m

    JSONObject resJsA = new JSONObject(resAuth);
    return resJsA.getString("access_token");
}

From source file:com.aktios.appthree.server.service.OAuthAuthenticationService.java

public String login(String username, String password) throws JSONException {
    RestOperations rest = new RestTemplate();
    String resAuth = rest.getForObject(oauthServerBaseURL + "/oauth/token?username=" + username + "&password="
            + password + "&client_id=" + appToken + "&client_secret=" + appPassword + "&grant_type=password",
            String.class);
    System.out.println(resAuth);//from   w ww.j a v  a 2s  .  c  o m

    JSONObject resJsA = new JSONObject(resAuth);
    return resJsA.getString("access_token");
}

From source file:com.aktios.appthree.server.service.OAuthAuthenticationService.java

public String loginSSO(String code, String redirectUri) throws JSONException {
    RestOperations rest = new RestTemplate();
    String resAuth = rest.getForObject(
            MessageFormat.format(
                    "{0}/oauth/token?code={1}&client_id={2}&client_secret={3}&grant_type={4}&redirect_uri={5}",
                    oauthServerBaseURL, code, appToken, appPassword, "authorization_code", redirectUri),
            String.class);
    System.out.println(resAuth);//from  ww w  .  j  a v a 2 s .c o  m
    JSONObject resJsA = new JSONObject(resAuth);
    return resJsA.getString("access_token");
}

From source file:nl.flotsam.greader.ReaderTemplate.java

@Override
public String getToken() {
    return doWithCallback(new ReaderCallback<String>() {
        @Override/*from  www . jav  a  2  s. co  m*/
        public String execute(RestOperations operations) {
            return operations.getForObject(TOKEN_URL, String.class);
        }
    });
}

From source file:nl.iwelcome.connector.google.GoogleAppsTemplate.java

@Override
public String getToken() {
    return doWithCallback(new ReaderCallback<String>() {
        @Override//w w  w  . j a v  a2 s .  c om
        public String execute(RestOperations operations) {
            return operations.getForObject(baseUrl, String.class);
        }
    });
}

From source file:org.cloudfoundry.identity.uaa.integration.TestAccountSetup.java

private boolean userAccountExists(RestOperations client) {
    Map<?, ?> map = client.getForObject(
            serverRunning.getUsersUri() + "?filter=userName eq '" + testAccounts.getUserName() + "'",
            Map.class);
    Integer count = (Integer) map.get("totalResults");
    if (count == null) {
        throw new IllegalStateException("Null response from user exists query: " + map);
    }//www. ja v a2s.  co  m
    if (count > 1) {
        throw new IllegalStateException("More than one user already exists with the test user name.");
    }
    if (count == 1) {
        return true;
    }
    return false;
}

From source file:com.surevine.alfresco.presence.openfire.OpenFireXmlPresenceServiceTest.java

@SuppressWarnings("unchecked")
private Presence getStatusForResponse(OpenFireXmlPresenceService service, String response) {
    RestOperations rest = mock(RestOperations.class);
    when(rest.getForObject(any(URI.class), any(Class.class)))
            .thenReturn(new StreamSource(new StringReader(response)));
    if (service == null) {
        service = new OpenFireXmlPresenceService();
        service.setHost("example.org");
        service.setUrl("http://example.org/presence");
    }/*from   ww w.  j a v a 2 s.  com*/
    service.setRestOperations(rest);
    return service.getUserPresence("someone");
}