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.”
}
}