springboot集成swagger3與knife4j的詳細代碼
springboot集成swagger3
swagger3的springboot啟動器jar包
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
註意:當前SpringBoot版本為2.5.6,Swagger3.0目前無法完全兼容SpringBoot2.6.x!
編寫TestController代碼
@RestController @RequestMapping("test") public class TestController { @GetMapping public Map<String, Object> get(@RequestParam String id) { Map<String, Object> r = new HashMap<>(1); r.put("id", id); return r; } @PostMapping public Map<String, Object> post() { Map<String, Object> r = new HashMap<>(1); r.put("code", 200); return r; } @PutMapping public Map<String, Object> put(String id) { Map<String, Object> r = new HashMap<>(1); r.put("id", id); return r; } @DeleteMapping public Map<String, Object> delete(String id) { Map<String, Object> r = new HashMap<>(1); r.put("id", id); return r; } }
創建Swagger3Configuration
@Configuration @EnableOpenApi public class SwaggerConfig { private static final String VERSION = "0.0.1"; @Bean public Docket createRestApi() { return new Docket(DocumentationType.OAS_30) .groupName("分組名稱") .apiInfo(apiInfo()) .select() //要掃描的包 .apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() // 設置標題 .title("文檔標題") //聯系人 .contact(contact()) //描述 .description("xxx文檔") //服務 .termsOfServiceUrl("https://spring.io/") //許可證 .license("Apache 2.0") .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0") .version(VERSION) .build(); } private Contact contact (){ return new Contact("SpringBoot", "https://spring.io/", "email"); } }
運行演示
訪問網址:http://127.0.0.1:8081/swagger-ui/index.html
對接口進行註解
swagger中常用的註解
註解 | 作用 | 使用位置 |
---|---|---|
@Api | 表示對類的說明常用參數 | 類 |
@ApiOperation | 說明方法的用途 | 方法 |
@ApiImplicitParams | 裡面可以包含多個@ApiImplicitParam | 方法 |
@ApiImplicitParam | 描述參數的用途 | 方法 |
@ApiModel | 表示一個數據類的信息 | 類 |
@ApiModelProperty | 描述數據類的屬性 | 屬性 |
@ApiIgnore | 忽略某個字段使之不顯示在文檔中 | 屬性 |
接口基本使用
1.新建一個用戶實體類
@ApiModel("用戶") @Data public class User { @ApiModelProperty("用戶名") private String username; @ApiModelProperty("密碼") private String password; }
2.給TestController添加註解
@Api(tags = "測試接口") @RestController @RequestMapping("test") public class TestController { @ApiOperation("get請求") @GetMapping @ApiImplicitParam(name = "id", value = "測試用id", dataTypeClass = String.class) public Map<String, Object> get(@RequestParam String id) { Map<String, Object> r = new HashMap<>(1); r.put("id", id); return r; } @ApiOperation("post請求") @PostMapping public Map<String, Object> post(@RequestBody User user) { Map<String, Object> r = new HashMap<>(1); r.put("code", 200); return r; } @ApiOperation("put請求") @PutMapping @ApiImplicitParam(name = "id", value = "put請求id", dataTypeClass = String.class) public Map<String, Object> put(String id) { Map<String, Object> r = new HashMap<>(1); r.put("id", id); return r; } @ApiOperation("delete請求") @DeleteMapping @ApiImplicitParam(name = "id", value = "delete請求id", dataTypeClass = String.class) public Map<String, Object> delete(String id) { Map<String, Object> r = new HashMap<>(1); r.put("id", id); return r; } }
運行結果
集成更好的UI-knife4j
maven
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-micro-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>
啟動器
在SwaggerConfig類上添加**@EnableKnife4j**
@Configuration @EnableOpenApi @EnableKnife4j public class SwaggerConfig
訪問網站:http://127.0.0.1:8080/doc.html#/home
到此這篇關於springboot集成swagger3與knife4j的文章就介紹到這瞭,更多相關springboot集成swagger3內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- SpringBoot集成Swagger3的實現
- 關於springboot集成swagger及knife4j的增強問題
- Spring boot配置 swagger的示例代碼
- 解決springboot引入swagger2不生效問題
- Springboot集成swagger實現方式