-
代码结构
-
pom.xml 依赖
-
Class 详细
@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {private String type;
private Value value;public Quote() {
}@Override
public String toString() {
return “Quote{” +
“type=’” + type + ‘’’ +
“, value=” + value +
‘}’;
}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;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Value {
private Long id;
private String quote;
public Value() {
}
@Override
public String toString() {
return "Value{" +
"id=" + id +
", quote='" + quote + '\'' +
'}';
}
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;
}
}
-
Application 主程序
@SpringBootApplication
public class ApplicationMain {private static final Logger log = LoggerFactory.getLogger(ApplicationMain.class);
public static void main(String[] args) {
SpringApplication.run(ApplicationMain.class);
}@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {consumeRestfulService(restTemplate); return null;
}
private void consumeRestfulService(RestTemplate restTemplate) {
for (int i = 0; i < 20; i++) {
Quote quote = restTemplate.getForObject(
“http://gturnquist-quoters.cfapps.io/api/random”, Quote.class);
log.info(quote.toString());
}
}
}
// 调用20次,查看测试返回结果
5. 测试结果