Swagger UI初識

新項目使用Swagger UI自動生成接口文檔,不需要頻繁更新接口文檔,保證接口文檔與代碼的一致,值得學習。本文記錄swaggerUi與springboot整合的步驟。

依賴添加

        <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>

新增swagger配置類

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                //apiInfo指定測試文檔基本信息,這部分將在頁面展示
                .apiInfo(apiInfo())
                .select()
                //apis() 控制哪些接口暴露給swagger,
                // RequestHandlerSelectors.any() 所有都暴露
                // RequestHandlerSelectors.basePackage("com.info.*")  指定包位置
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    //基本信息,頁面展示
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("測試項目標題")
                .description("接口描述")
                //聯系人實體類
                .contact(
                        new Contact("名字", "網址", "郵箱")
                )
                //版本號
                .version("1.0.0-SNAPSHOT")
                .build();
    }
}

在Controller類上增加swagger配置

@RestController
@RequestMapping
//Api注解,描述信息 可通過tag進行分類
@Api(value = "HelloController", description = "HelloController")
public class HelloController {
    @PostMapping("/addPerson")
    //方法描述
    @ApiOperation(notes = "添加人員", value = "addPerson")
    public Person addPerson(
            @ApiParam(name = "name", value = "姓名") @RequestParam("name") String name,
            @ApiParam(name = "age", value = "年齡")  @RequestParam("age") Integer age) {
        Person person = new Person();
        person.setAge(age);
        person.setName(name);

        return repository.save(person);
    }
}

以上配置完成之后,直接啟動項目,訪問地址:localhost:8080/swagger-ui.html,即可打開如下頁面


image.png

Controller下的所有接口得到展示。展開其中一個可以看到接口詳情:


image.png

十分強大的工具,只需簡單注解即可生成接口文檔,代碼入侵小。
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 今天技術總監說:小明,我們本次3.0改造,使用swagger2.0作為前后端分離的接口規范,它可以一鍵生成前后端的...
    coder小明閱讀 3,419評論 4 12
  • 背景 我們公司接口管理工具用的是Yapi,但是Yapi還是需要后端開發人員分配較多額外的精力去維護接口文檔的信息,...
    tuser閱讀 9,495評論 0 11
  • 2017/04/07 倚著窗子坐著,風吹過不想動彈。不敢看窗外,我想著,外面應該是竹林翠環,溪流幽徑,蟲鳥爭喧的山...
    SameFam閱讀 253評論 0 1
  • 1121 楊博 北京 【每日一結構】結構思考力21天思維改善訓練營 G:【閱讀感悟】我們要學會殘酷地自我衡量,盲目...
    丑丑的小鴨閱讀 292評論 0 0
  • 題: class ChangeIt { static void doIt( int[] z ) { ...
    daysting閱讀 958評論 0 1