日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot 中使用 Swagger2 构建强大的 RESTful API 文档

發布時間:2024/4/17 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot 中使用 Swagger2 构建强大的 RESTful API 文档 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

項目現狀:由于前后端分離,沒有很好的前后端合作工具。

由于接口眾多,并且細節復雜(需要考慮不同的HTTP請求類型、HTTP頭部信息、HTTP請求內容等),高質量地創建這份文檔本身就是件非常吃力的事,下游的抱怨聲不絕于耳。

隨著時間推移,不斷修改接口實現的時候都必須同步修改接口文檔,而文檔與代碼又處于兩個不同的媒介,除非有嚴格的管理機制,不然很容易導致不一致現象。

? ?為了解決上面這樣的問題,本文將介紹RESTful API的重磅好伙伴Swagger2,它可以輕松的整合到Spring Boot中,并與Spring MVC程序配合組織出強大RESTful API文檔。它既可以減少我們創建文檔的工作量,同時說明內容又整合入實現代碼中,讓維護文檔和修改代碼整合為一體,可以讓我們在修改代碼邏輯的同時方便的修改文檔說明。另外Swagger2也提供了強大的頁面測試功能來調試每個RESTful API。

?

添加Swagger2依賴

在pom.xml中加入Swagger2的依賴

1 2 3 4 5 6 7 8 9 10 <dependency> ????<groupId>io.springfox</groupId> ????<artifactId>springfox-swagger2</artifactId> ????<version>2.2.2</version> </dependency> <dependency> ????<groupId>io.springfox</groupId> ????<artifactId>springfox-swagger-ui</artifactId> ????<version>2.2.2</version> </dependency>

創建Swagger2配置類

在Application.java同級創建Swagger2的配置類Swagger2。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @Configuration @EnableSwagger2 public class Swagger2 { ????@Bean ????public Docket createRestApi() { ????????return new Docket(DocumentationType.SWAGGER_2) ????????????????.apiInfo(apiInfo()) ????????????????.select()? ? ? ? ? ? ? ??.apis(RequestHandlerSelectors.basePackage("com.bai.controller ")) ????????????????.paths(PathSelectors.any()) ????????????????.build(); ????} ????private ApiInfo apiInfo() { ????????return new ApiInfoBuilder() ????????????????.title("Spring Boot中使用Swagger2構建RESTful APIs") ????????????????.description("更多Spring Boot相關文章請關注:swagger官網") ????????????????.termsOfServiceUrl("https://swagger.io/") ????????????????.contact("程序猿DD") ????????????????.version("1.0") ????????????????.build(); ????} }

如上代碼所示,通過@Configuration注解,讓Spring來加載該類配置。再通過@EnableSwagger2注解來啟用Swagger2。

?

測試代碼:

package com.bai.controller;import com.bai.domain.User; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*;import java.util.*;/*** Created by DELL on 2017/7/5.*/ @RestController @RequestMapping(value = "/users") // 通過這里配置使下面的映射都在/users下,可去除 public class UsersController {static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());@ApiOperation(value = "獲取用戶列表", notes = "")@RequestMapping(value = {""}, method = RequestMethod.GET)public List<User> getUserList() {List<User> r = new ArrayList<User>(users.values());return r;}@ApiOperation(value = "創建用戶", notes = "根據User對象創建用戶")@ApiImplicitParam(name = "user", value = "用戶詳細實體user", required = true, dataType = "User")@RequestMapping(value = "", method = RequestMethod.POST)public String postUser(@RequestBody User user) {users.put(user.getId(), user);return "success";}@ApiOperation(value = "獲取用戶詳細信息", notes = "根據url的id來獲取用戶詳細信息")@ApiImplicitParam(name = "id", value = "用戶ID", required = true, paramType = "path", dataType = "Long")@RequestMapping(value = "/{id}", method = RequestMethod.GET)public User getUser(@PathVariable Long id) {return users.get(id);}@ApiOperation(value = "更新用戶詳細信息", notes = "根據url的id來指定更新對象,并根據傳過來的user信息來更新用戶詳細信息")@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用戶ID", required = true, paramType = "path", dataType = "Long"),@ApiImplicitParam(name = "user", value = "用戶詳細實體user", required = true, dataType = "User")})@RequestMapping(value = "/{id}", method = RequestMethod.PUT)public String putUser(@PathVariable Long id, @RequestBody User user) {User u = users.get(id);u.setUserName(user.getUserName());u.setEmail(user.getEmail());users.put(id, u);return "success";}@ApiOperation(value = "刪除用戶", notes = "根據url的id來指定刪除對象")@ApiImplicitParam(name = "id", value = "用戶ID", required = true,paramType="path", dataType = "Long")@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)public String deleteUser(@PathVariable Long id) {users.remove(id);return "success";} }

  

完成上述代碼添加上,啟動Spring Boot程序,訪問:http://localhost:8080/swagger-ui.html
。就能看到前文所展示的RESTful API的頁面。

?

轉載于:https://www.cnblogs.com/baizhanshi/p/7124575.html

總結

以上是生活随笔為你收集整理的Spring Boot 中使用 Swagger2 构建强大的 RESTful API 文档的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。