Springboot 2 官方文档练习
Springboot 2 官方文档练习
- 官方文档地址 https://spring.io/guides
- 使用OpenJDK 11 创建Springboot 2 目前木有遇到问题。
Building a RESTful Web Service
pom.xml
<wiz_code_mirror>
16
1
<dependency>
2
<groupId>org.springframework.boot</groupId>
3
<artifactId>spring-boot-starter-web</artifactId>
4
</dependency>
5
6
<dependency>
7
<groupId>org.springframework.boot</groupId>
8
<artifactId>spring-boot-starter-test</artifactId>
9
<scope>test</scope>
10
<exclusions>
11
<exclusion>
12
<groupId>org.junit.vintage</groupId>
13
<artifactId>junit-vintage-engine</artifactId>
14
</exclusion>
15
</exclusions>
16
</dependency>
创建实体类 Greeting
<wiz_code_mirror>
17
1
public class Greeting {
2
private final long id;
3
private final String content;
4
5
public Greeting(long id, String content) {
6
this.id = id;
7
this.content = content;
8
}
9
10
public long getId() {
11
return id;
12
}
13
14
public String getContent() {
15
return content;
16
}
17
}
创建 GreetingController
<wiz_code_mirror>
11
1
2
public class GreetingController {
3
4
private static final String template = "Hello, %s!";
5
private final AtomicLong counter = new AtomicLong();
6
// http://localhost:8080/greeting?name=surich
7
("/greeting")
8
public Greeting greeting((value = "name", defaultValue = "World") String name) {
9
return new Greeting(counter.incrementAndGet(), String.format(template, name));
10
}
11
}
Consuming a RESTful Web Service
pom.xml
<wiz_code_mirror>
18
1
<dependencies>
2
<dependency>
3
<groupId>org.springframework.boot</groupId>
4
<artifactId>spring-boot-starter-web</artifactId>
5
</dependency>
6
7
<dependency>
8
<groupId>org.springframework.boot</groupId>
9
<artifactId>spring-boot-starter-test</artifactId>
10
<scope>test</scope>
11
<exclusions>
12
<exclusion>
13
<groupId>org.junit.vintage</groupId>
14
<artifactId>junit-vintage-engine</artifactId>
15
</exclusion>
16
</exclusions>
17
</dependency>
18
</dependencies>
创建实体类 Value
<wiz_code_mirror>
34
1
(ignoreUnknown = true)
2
public class Value {
3
4
private Long id;
5
private String quote;
6
7
public Value() {
8
}
9
10
public Long getId() {
11
return this.id;
12
}
13
14
public String getQuote() {
15
return this.quote;
16
}
17
18
public void setId(Long id) {
19
this.id = id;
20
}
21
22
public void setQuote(String quote) {
23
this.quote = quote;
24
}
25
26
27
public String toString() {
28
return "Value{" +
29
"id=" + id +
30
", quote='" + quote + '\'' +
31
'}';
32
}
33
}
34
创建实体类 Quote
<wiz_code_mirror>
33
1
(ignoreUnknown = true)
2
public class Quote {
3
4
private String type;
5
private Value value;
6
7
public Quote() {
8
}
9
10
public String getType() {
11
return type;
12
}
13
14
public void setType(String type) {
15
this.type = type;
16
}
17
18
public Value getValue() {
19
return value;
20
}
21
22
public void setValue(Value value) {
23
this.value = value;
24
}
25
26
27
public String toString() {
28
return "Quote{" +
29
"type='" + type + '\'' +
30
", value=" + value +
31
'}';
32
}
33
}
启动类 ConsumingRestfulWebServiceApplication
<wiz_code_mirror>
1
2
public class ConsumingRestfulWebServiceApplication {
3
4
private static final Logger log = LoggerFactory.getLogger(ConsumingRestfulWebServiceApplication.class);
5
6
public static void main(String[] args) {
7
SpringApplication.run(ConsumingRestfulWebServiceApplication.class, args);
8
}
9
10
11
public RestTemplate restTemplate(RestTemplateBuilder builder) {
12
return builder.build();
13
}
14
15
16
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
17
return args -> {
18
Quote quote = restTemplate.getForObject("https://gturnquist-quoters.cfapps.io/api/random", Quote.class);
19
log.info(quote.toString());
20
};
21
}
22
}
posted on 2020-02-24 12:51 allforcloud 阅读(527) 评论(0) 收藏 举报
浙公网安备 33010602011771号