Starting with Spring Initializr![](https://img-blog.csdnimg.cn/20210619100443730.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzU5MzQ4NDc5,size_16,color_FFFFFF,t_70)
![](https://img-blog.csdnimg.cn/20210619100353200.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzU5MzQ4NDc5,size_16,color_FFFFFF,t_70)
Fetching a REST Resource
First, you need to create a domain class to contain the data that you need. The following listing shows the Quote
class, which you can use as your domain class:
src/main/java/com/example/consumingrest/Quote.java
copy
package com.example.consumingrest;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {
private String type;
private Value value;
public Quote() {
}
public String getType() {
return type;
You also need an additional class, to embed the inner quotation itself. The Value
class fills that need and is shown in the following listing (at src/main/java/com/example/consumingrest/Value.java
)
copy
package com.example.consumingrest;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Value {
private Long id;
private String quote;
public Value() {
}
public Long getId() {
return this.id;
}
public String getQuote() {
return this.quote;
}
public void setId(Long id) {
this.id = id;
}
public void setQuote(String quote) {
this.quote = quote;
}
@Override
public String toString() {
return "Value{" +
"id=" + id +
", quote='" + quote + '\'' +
'}';
}
}
Finishing the Application
Now you need to add a few other things to the ConsumingRestApplication
class to get it to show quotations from our RESTful source. You need to add:
-
A logger, to send output to the log (the console, in this example).
-
A
RestTemplate
, which uses the Jackson JSON processing library to process the incoming data. -
A
CommandLineRunner
that runs theRestTemplate
(and, consequently, fetches our quotation) on startup.
The following listing shows the finished ConsumingRestApplication
class (at src/main/java/com/example/consumingrest/ConsumingRestApplication.java
package com.example.consumingrest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class ConsumingRestApplication {
private static final Logger log = LoggerFactory.getLogger(ConsumingRestApplication.class);
public static void main(String[] args) {
SpringApplication.run(ConsumingRestApplication.class, args);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
Quote quote = restTemplate.getForObject(
"https://quoters.apps.pcfone.io/api/random", Quote.class);
log.info(quote.toString());
};
}
}
run
mvnw spring-boot:run
public void setType(String type) {
this.type = type;
}
public Value getValue() {
return value;
}
public void setValue(Value value) {
this.value = value;
}
@Override
public String toString() {
return "Quote{" +
"type='" + type + '\'' +
", value=" + value +
'}';
}
}