Skip to content

Examples

Michael Pratt edited this page Mar 4, 2016 · 5 revisions

Examples

Below are some example usages of robusto framework.

Static URL w/ Spring Rest

This example shows a simple client that makes a POST call to a remote service using the ConstantUriProvider, which uses the same URI for every call (including retries). The client extends SpringRestClient and thus can easily call getRestTemplate() to get a fully initialized RestTemplate object to use for all of its remote calls. It uses a simple String for return types, but could easily be modified to use the Spring ResponseEntity type or even a custom DTO. Note that it uses the synchronous execution mode of Hystrix.

public class MyClient extends SpringRestClient
{
    public String createEntity(SomeDto body)
    {
        ApiCommand<String> command = restCommand(
            new ConstantUriProvider<String>("https://api.google.com/v1/someservice"),
            new SpringInstanceCallback<String>()
            {
                @Override
                public String runWithUrl(String url)
                {
                    return getRestTemplate().postForObject(
                        UriComponentsBuilder
                            .fromUriString(url)
                            .pathSegment("new").pathSegment("resource")
                            .build()
                            .toUriString(),
                        body,
                        String.class
                    );
                }
            }
        ).build();
        
        return command.execute();
    }
}
Clone this wiki locally