Skip to content

Commit d4f85dd

Browse files
committed
构建 RESTful Web 服务
1 parent 677080a commit d4f85dd

File tree

6 files changed

+157
-1
lines changed

6 files changed

+157
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
### 基础
3030

31-
1. [构建 RESTful Web 服务]()
31+
1. **[构建 RESTful Web 服务](./docs/basis/sringboot-restful-web-service.md)**
3232
2. **[RestController VS Controller](./docs/basis/RestControllerVSController.md)**
3333
3. [使用 spring-boot-devtools 进行热部署](./docs/basis/spring-boot-devtools.md)
3434
4. [整合 SpringBoot+Mybatis](./docs/basis/springboot-mybatis.md)[SpirngBoot2.0+ 的 SpringBoot+Mybatis 多数据源配置](./docs/basis/springboot-mybatis-mutipledatasource.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
本节我们将开发一个简单的 RESTful Web 服务。
2+
3+
RESTful Web 服务与传统的 MVC 开发一个关键区别是返回给客户端的内容的创建方式:**传统的 MVC 模式开发会直接返回给客户端一个视图,但是 RESTful Web 服务一般会将返回的数据以 JSON 的形式返回,这也就是现在所推崇的前后端分离开发。**
4+
5+
为了节省时间,本篇内容的代码是在 **[Spring Boot 版 Hello World & Spring Boot 项目结构分析](https://snailclimb.gitee.io/springboot-guide/#/./start/springboot-hello-world)** 基础上进行开发的。
6+
7+
通过下面的内容你将学习到下面这些东西:
8+
9+
1. Lombok 优化代码利器
10+
2. `@RestController`
11+
3. `@RequestParam`以及`@Pathvairable`
12+
4. `@RequestMapping`` @GetMapping`......
13+
14+
因为本次开发用到了 Lombok 这个简化 Java 代码的工具,所以我们需要在 pom.xml 中添加相关依赖。
15+
16+
```xml
17+
<dependency>
18+
<groupId>org.projectlombok</groupId>
19+
<artifactId>lombok</artifactId>
20+
<version>1.18.10</version>
21+
</dependency>
22+
```
23+
24+
并且你需要下载 IDEA 中支持 lombok 的插件:
25+
26+
![ IDEA 中下载支持 lombok 的插件](https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/2019-7/lombok-idea.png)
27+
28+
假如我们有一个书架,上面放了很多书。为此,我们需要新建一个 `Book` 实体类。
29+
30+
`com.example.helloworld.entity`
31+
32+
```java
33+
/**
34+
* @author shuang.kou
35+
*/
36+
@Data
37+
public class Book {
38+
private String name;
39+
private String description;
40+
}
41+
```
42+
43+
我们还需要一个控制器对书架上进行添加、查找以及查看。为此,我们需要新建一个 `BookController`
44+
45+
```java
46+
import com.example.helloworld.entity.Book;
47+
import org.springframework.http.ResponseEntity;
48+
import org.springframework.web.bind.annotation.DeleteMapping;
49+
import org.springframework.web.bind.annotation.GetMapping;
50+
import org.springframework.web.bind.annotation.PathVariable;
51+
import org.springframework.web.bind.annotation.PostMapping;
52+
import org.springframework.web.bind.annotation.RequestBody;
53+
import org.springframework.web.bind.annotation.RequestParam;
54+
import org.springframework.web.bind.annotation.RestController;
55+
56+
import java.util.ArrayList;
57+
import java.util.List;
58+
import java.util.stream.Collectors;
59+
60+
@RestController
61+
@RequestMapping("/api")
62+
public class BookController {
63+
64+
private List<Book> books = new ArrayList<>();
65+
66+
@PostMapping("/book")
67+
public ResponseEntity<List<Book>> addBook(@RequestBody Book book) {
68+
books.add(book);
69+
return ResponseEntity.ok(books);
70+
}
71+
72+
@DeleteMapping("/book/{id}")
73+
public ResponseEntity deleteBookById(@PathVariable("id") int id) {
74+
books.remove(id);
75+
return ResponseEntity.ok(books);
76+
}
77+
78+
@GetMapping("/book")
79+
public ResponseEntity getBookByName(@RequestParam("name") String name) {
80+
List<Book> results = books.stream().filter(book -> book.getName().equals(name)).collect(Collectors.toList());
81+
return ResponseEntity.ok(results);
82+
}
83+
}
84+
```
85+
86+
1. `@RestController` 将返回的对象数据直接以 JSON 或 XML 形式写入 HTTP 响应(Response)中。绝大部分情况下都是直接以 JSON 形式返回给客户端,很少的情况下才会以 XML 形式返回。转换成 XML 形式还需要额为的工作,上面代码中演示的直接就是将对象数据直接以 JSON 形式写入 HTTP 响应(Response)中。
87+
2. `@RequestMapping` :上面的示例中没有指定 GET 与 PUT、POST 等,因为@RequestMapping默认映射所有HTTP Action,你可以使用`@RequestMapping(method=ActionType)`来缩小这个映射。
88+
3. ` @PostMapping`实际上就等价于 `@RequestMapping(method = RequestMethod.POST)`,同样的 ` @DeleteMapping` ,`@GetMapping`也都一样,常用的 HTTP Action 都有一个这种形式的注解所对应。
89+
4. `@PathVariable` :取url地址中的参数。`@RequestParam("name") ` url的查询参数值。
90+
91+
92+

start/hello-world/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
<artifactId>spring-boot-starter-test</artifactId>
3030
<scope>test</scope>
3131
</dependency>
32+
<dependency>
33+
<groupId>org.projectlombok</groupId>
34+
<artifactId>lombok</artifactId>
35+
<version>1.18.10</version>
36+
</dependency>
3237
</dependencies>
3338

3439
<build>

start/hello-world/src/main/java/com/example/helloworld/HelloWorldApplication.java

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55

6+
/**
7+
* @author shuang.kou
8+
*/
69
@SpringBootApplication
710
public class HelloWorldApplication {
811

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.example.helloworld.controller;
2+
3+
import com.example.helloworld.entity.Book;
4+
import org.springframework.http.MediaType;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.DeleteMapping;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.RequestBody;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestMethod;
13+
import org.springframework.web.bind.annotation.RequestParam;
14+
import org.springframework.web.bind.annotation.RestController;
15+
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
import java.util.stream.Collectors;
19+
20+
@RestController
21+
@RequestMapping(value = "/api",method = RequestMethod.POST)
22+
public class BookController {
23+
24+
private List<Book> books = new ArrayList<>();
25+
26+
@PostMapping(value = "/book")
27+
public ResponseEntity<List<Book>> addBook(@RequestBody Book book) {
28+
books.add(book);
29+
return ResponseEntity.ok(books);
30+
}
31+
32+
@DeleteMapping("/book/{id}")
33+
public ResponseEntity deleteBookById(@PathVariable("id") int id) {
34+
books.remove(id);
35+
return ResponseEntity.ok(books);
36+
}
37+
38+
@GetMapping("/book")
39+
public ResponseEntity getBookByName(@RequestParam("name") String name) {
40+
List<Book> results = books.stream().filter(book -> book.getName().equals(name)).collect(Collectors.toList());
41+
return ResponseEntity.ok(results);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.helloworld.entity;
2+
3+
4+
import lombok.Data;
5+
6+
/**
7+
* @author shuang.kou
8+
*/
9+
@Data
10+
public class Book {
11+
private String name;
12+
private String description;
13+
}

0 commit comments

Comments
 (0)