第一步(构建springboot项目)

第二步(创建实体类)

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/** @JsonIgnoreProperties(ignoreUnknown=true) : 将这个注解写在类上之后,就会忽略类中不存在的字段。
*这个注解还可以指定要忽略的字段,例如@JsonIgnoreProperties({ “password”, “secretKey” })
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {
    private String type;
    private Value value;

    public Quote() {
    }

    public String getType() {
        return type;
    }

    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 +
                '}';
    }
}
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/** @JsonIgnoreProperties(ignoreUnknown=true) : 将这个注解写在类上之后,就会忽略类中不存在的字段。
 *这个注解还可以指定要忽略的字段,例如@JsonIgnoreProperties({ “password”, “secretKey” })
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class Value {
    private Long id;
    private String quote;

    public Value() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getQuote() {
        return quote;
    }

    public void setQuote(String quote) {
        this.quote = quote;
    }

    @Override
    public String toString() {
        return "Value{" +
                "id=" + id +
                ", quote='" + quote + '\'' +
                '}';
    }
}

第三步(向ConsumingRestApplication类添加一些其他内容)

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 LOGGER= LoggerFactory.getLogger(ConsumingRestApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(ConsumingRestApplication.class, args);
    }
/**
 * @Bean :把RestTemplate托管给spring
 * RestTemplate :HTTP 请求工具
  */
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder){
        return builder.build();
    }
/**
 * @Bean :把CommandLineRunner托管给spring
 * CommandLineRunner :项目启动后执行的功能
  */
    @Bean
    public CommandLineRunner run(RestTemplate restTemplate) throws Exception{
        return args -> {
            Quote quote=restTemplate.getForObject("https://quoters.apps.pcfone.io/api/random",Quote.class);
            LOGGER.info(quote.toString());
        };
    }
}

第四步(测试)