Springboot集成Swagger2


Swagger2簡介

1.隨項目自動生成強大RESTful API文檔,減少工作量
2.API文檔與代碼整合在一起,便於同步更新API說明
3.頁面測試功能來調試每個RESTful API

springboot集成Swagger2步驟

1. 新建一個Springboot項目

在這里插入圖片描述

2. 導入依賴

在這里插入圖片描述

		  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
		 <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

3. 編寫控制器

在這里插入圖片描述

@RestController
public class Mycontroller {

    @GetMapping({"/","hello"})
    public String hello(){
        return "helloword";
    }
}

4. 編寫swagger的配置類

寫上注解
@Configuration
@EnableSwagger2
在這里插入圖片描述

@Configuration
@EnableSwagger2
public class SwaggerConfig {
}

啟動項目運行
瀏覽器輸入 http://localhost:8080/swagger-ui.html
在這里插入圖片描述
我們所寫的controller方法都會被檢測到

5. 配置Swagger2

  1. 配置Swagger信息 info
    在這里插入圖片描述
    @Bean
    public Docket docket() {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apinfo());
    }
    public ApiInfo apinfo(){
        Contact contact = new Contact(
                "joker_dj",
                "https://www.cnblogs.com/joker-dj/",
                "2332810801@qq.com");
        return new ApiInfo("joker_dj的api文檔",
                "Api Documentation",
                "1.0",
                "https://www.cnblogs.com/joker-dj/",
                contact, "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

運行看效果

在這里插入圖片描述
2. 配置多個分組
多配置幾個Docket即可
在這里插入圖片描述
在這里插入圖片描述
3. 配置其他的信息 不一一解釋 都有注釋
在這里插入圖片描述

@Bean
    public Docket docket(){

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apinfo())
                //是否啟用swagger false不啟用
                .enable(true)
                .groupName("joker_dj")
                .select()
                /*
                配置RequestHandlerSelectors 掃描接口的方式
                指定掃描的包basePackage
                any()掃描全部
                none()不掃描
                withClassAnnotation:掃描類上的注解
                */
                .apis(RequestHandlerSelectors.basePackage("com.dj.controller"))
                //過濾路徑 比如PathSelectors.ant("login/**")
               // .paths(PathSelectors.ant(""))
                .build();
    }

6.添加注釋信息

  1. 編寫user實體
    在這里插入圖片描述
  2. 在controller返回一個user
    在這里插入圖片描述
    運行
    在這里插入圖片描述
    會檢測到我們所有寫的controller方法
    也可以進行測試方法
    點擊Try it out
    在這里插入圖片描述
  3. 點擊運行
    在這里插入圖片描述
    這里就會返回我們測試接口的信息以及響應的結果
    在這里插入圖片描述
  4. 給類,屬性和方法添加注釋信息
    在user實體類添加注解
    @ApiModel:給類添加注釋
    @ApiModelProperty :給字段添加注釋
    在這里插入圖片描述
@ApiModel("用戶實體類")
public class user {
    @ApiModelProperty("用戶名")
    public String username;
    @ApiModelProperty("密碼")
    public String password;
}

運行看效果
在這里插入圖片描述
添加到controller上
@ApiOperation
在這里插入圖片描述
運行看效果
在這里插入圖片描述
5. 發送數據 測試接口
在controller中編寫代碼
在這里插入圖片描述
運行測試
在這里插入圖片描述
輸入用戶名 密碼 點擊測試
在這里插入圖片描述
在這里插入圖片描述
這樣就返回了我們數據

總結:Swagger這個工具主要是針對開發人員測試接口來使用的,
注意:在項目上線的時候己得吧Swagger給關閉 防止接口暴露 (安全)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM