SpringBoot簡單介紹已經有一篇相關博客,大家可以參考這里,本篇博客主要SpringBoot實戰Swagger與RESTful的開發。接下來進入正題:
Jar的相關版本為:
- Spring Boot 2.0.2
- Swagger 2.8.0
Swagger簡介
- 對于Swagger的理解,其實就是一個工具,是一個構建API的工具。根據Controller整理出對應的API,當然還可以直接在Swagger-UI上測試。
RESTful簡介
- 上面說了Swagger是構建API的工具,而RESTful 是目前最流行的一種互聯網軟件架構! REST(Representational State Transfer,表述性狀態轉移)一詞是由 Roy Thomas Fielding 在他 2000 年博士論文中提出的,定義了他對互聯網軟件的架構原則,如果一個架構符合 REST 原則,則稱它為 RESTful 架構。具體介紹,大家可以參考這里。
Spring Boot 對 RESTful 的支持
Spring Boot 全面支持開發 RESTful 程序,通過不同的注解來支持前端的請求,除了經常使用的注解外,Spring Boot 還提了一些組合注解。這些組合注解就是我們使用的 @RequestMapping 的簡寫版.
- @GetMapping,處理 Get 請求
- @PostMapping,處理 Post 請求
- @PutMapping,用于更新資源
- @DeleteMapping,處理刪除請求
@GetMapping(value="/xxx")
等價于
@RequestMapping(value = "/xxx",method = RequestMethod.GET)
@PostMapping(value="/xxx")
等價于
@RequestMapping(value = "/xxx",method = RequestMethod.POST)
.....
Coding
接下來對具體代碼進行分析!
1. 相關依賴如下:
<properties>
<java.version>1.8</java.version>
<springfox.version>2.8.0</springfox.version>
</properties>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.version}</version>
</dependency>
2. Swagger配置文件
@Configuration
public class SwaggerConfig {
@Value("${swagger.enabled}")
private boolean swaggerEnabled;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(swaggerEnabled).select()
.apis(RequestHandlerSelectors.basePackage("com.jtcoding.chat")).paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Simple Chat-Server RESTful APIs")
.description("Simple Chat-Server Swagger2 RESTful APIs").version("1.0").build();
}
}
3. 在啟動類中加入 @EnableSwagger2 注解
@EnableSwagger2
@SpringBootApplication
public class ChatApplication {
public static void main(String[] args) {
SpringApplication.run(ChatApplication.class, args);
}
}
4. 在application.properties配置文件中,配置swagger的開關
swagger.enabled=true
5. 接下來編寫RESTful風格API,下面給一個比較全面一點的API:
@Api(tags = "好友相關")
@RestController
@RequestMapping("/friends")
public class FriendController {
@Autowired
private FriendService friendService;
@ApiOperation(value = "獲取好友列表", notes = "獲取用戶的好友列表")
@GetMapping("/{userNum}")
public Result<List<User>> getFriendListByUserNum(@PathVariable Integer userNum) {
return Result.success(friendService.getFriendListByUserNum(userNum));
}
@ApiOperation(value = "搜索好友", notes = "搜索好友")
@GetMapping("/search/{username}")
public Result<User> searchUserByUsername(@PathVariable String username) {
return Result.success(friendService.searchUserByUsername(username));
}
@ApiOperation(value = "添加好友", notes = "添加好友")
@PostMapping("/requests")
public Result<Boolean> addFriendRequest(@RequestBody FriendRequest friendRequest) {
return Result.success(friendService.addFriendRequest(friendRequest));
}
@ApiOperation(value = "顯示好友申請列表", notes = "顯示好友申請列表")
@GetMapping("/requests/{userNum}")
public Result<List<User>> getUserByFriendReq(@PathVariable Integer userNum) {
return Result.success(friendService.getUserByFriendReq(userNum));
}
@ApiOperation(value = "接受好友申請", notes = "接受好友申請")
@PutMapping("/requests/accept")
public Result<User> acceptFriendRequest(@RequestBody FriendRequest friendRequest) {
return Result.success(friendService.acceptFriendRequest(friendRequest));
}
@ApiOperation(value = "拒絕好友申請", notes = "拒絕好友申請")
@PutMapping("/requests/reject")
public Result<Boolean> rejectFriendRequest(@RequestBody FriendRequest friendRequest) {
return Result.success(friendService.rejectFriendRequest(friendRequest));
}
}
6. 效果如下
-
整體效果:具體效果
-
具體某一個:點擊Try it out即可測試
點擊Try it out即可測試 -
測試效果如下:
具體詳情
總結
具體整合過程已經完成,相關細節方面就是:
- 給Swagger加一個開關,可以在配置文件中控制開啟或者關閉;
- RESTful API規范的設計;
- 本文有借鑒過一些文章,如有雷同,這里指明為借鑒。有不對之處,請指出,謝謝!