Spring-使用 RESTful 服务

目标

创建一个使用 Spring 的 RestTemplate 来接收一个随机的 Spring Boot 引文的应用程序,接收的请求为:
http://localhost:8080/api/random

Spring initializr 配置

https://start.spring.io
添加 Web 依赖即可,其余配置随意。

获取 REST 资源

下载随机生成引文的代码仓库:https://github.com/spring-guides/quoters
运行该程序,即可在浏览器中访问:

引文的格式大概如下:

1
2
3
4
5
6
7
{
type: "success",
value: {
id: 10,
quote: "Really loving Spring Boot, makes stand alone Spring apps easy."
}
}

RestTemplate 使得与RESTful service 的交互变得很简单。甚至可以将返回的数据和你自定义的域类型绑定。

具体来说,首先需要创建一个 domain class 来包含你所需的数据,所以创建 Quote 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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;
}

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 +
'}';
}
}

其中,@JsonIgnoreProperties 是 Jackson JSON 库中注解,用来指定json中任何不与该类绑定的数据类型都将被忽略掉。

为了将数据和自定义类型绑定,需要指定与 API 返回的 JSON 文档中 key 完全相同的变量名称。假如变量名称不相同,那么就需要用 @JsonProperty 注解来指定准确的 key。

所以,还需要另一个类 Value 来代表引文。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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 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 + '\'' +
'}';
}
}

修改主程序

主要添加以下几个内容:

  1. 一个 RestTemplate,用 Jackson JSON 库处理输入数据
  2. 一个 CommandLineRunner 来运行 RestTemplate
  3. 一个 logger 来打印日志,将获取到的引文打印出来
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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("http://localhost:8080/api/random", Quote.class);
log.info(quote.toString());
};
}
}

编译运行

  • 编译打包:./mvnw clean package
  • 运行:java -Dserver.port=8099 -jar target/consuming-rest-0.0.1-SNAPSHOT.jar
  • 运行时需另行指定端口,因为默认的 8080 端口在运行提供数据的 RESTful service(前面的 quoters)
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2022 qusong
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信