Consuming a RESTful Web Service
spring demo中给的接口是 http://gturnquist-quoters.cfapps.io/api/random.
返回的数据(用浏览器或者curl命令等):
{
type: "success",
value: {
id: 10,
quote: "Really loving Spring Boot, makes stand alone Spring apps easy."
}
}
Spring提供了一个方便的模板类,叫做RestTemplate。与大多数RestTemplate RESTful服务交互。
下面我们编写java beans:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {
private String type;
private Value value;
}
正如你可以看到,这是一个极少数的属性和匹配的getter方法简单的Java类。它有jackson JSON处理库jsonignoreproperties表明any properties not bound in this type should be ignored.
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Value {
private Long id;
private String quote;
}
现在我们用
RestTemplate来编写application 类
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.web.client.RestTemplate;
@SpringBootApplication
public class Application implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String args[]) {
SpringApplication.run(Application.class);
}
@Override
public void run(String... strings) throws Exception {
RestTemplate restTemplate = new RestTemplate();
Quote quote = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", Quote.class);
log.info(quote.toString());
}
}
因为Jackson JSON处理库是在classpath中,RestTemplate将使用它(通过消息转换器)转换传入的JSON数据到一个对象。
从那里,引用对象的内容将被log到控制台。
在这里,你使用RestTemplate接受HTTP GET请求。但RestTemplate还支持其他HTTP put post delete。
org.springframework.boot.CommandLineRunner;
接口用来指示SpringApplication包含的bean应在跑的时候执行。多commandlinerunner beans可以在同一应用程序的上下文中定义的,
可以责令使用 Ordered interface 或者 @Order注解。
使用maven构建项目
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-consuming-rest</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>