Spring Boot Tutorial (26) Consume Restful web services

Architecture engineering

Create a springboot project to consume RESTFUL services. This service is  http:///gturnquist-quoters.cfapps.io/api/random  and it returns random Json strings. 
In the Spring project, it provides a very simple class called RestTemplate, which can easily consume services.

consumer service

To consume services through RestTemplate, you need to register a RestTemplate bean in the context. code show as below:

@Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }

    @Bean
    public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
        return args -> {
            String quote = restTemplate.getForObject(
                    "http://gturnquist-quoters.cfapps.io/api/random", String.class);
            log.info(quote.toString());
        };
    }

Running the program, the console prints:

{ 
“type”: “success”, 
“value”: { 
“id”: 6, 
“quote”: “It embraces convention over configuration, providing an experience on par with frameworks that excel at early stage development, such as Ruby on Rails.” 
} 
}

source code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325175951&siteId=291194637