首頁 > 軟體

springboot整合swagger3與knife4j的詳細程式碼

2022-08-16 18:01:55

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內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com