Swagger使用簡(jiǎn)略

今天后端的朋友用了swagger管理接口,非常好用,記錄一下。

第一步:添加依賴

這個(gè)可以在任意一個(gè)pom.xml里面布置,我這里放在Controller的pom.xml里面

       <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>

第二步:把一個(gè)WebMvcConfigurer的實(shí)現(xiàn)類和啟動(dòng)項(xiàng)放在一起

image.png

在swagger2類中設(shè)置參數(shù)

@Configuration
@EnableSwagger2
public class Swagger2 implements WebMvcConfigurer {
    
    // 接口版本號(hào)
    private final String version = "1.0";
    // 接口大標(biāo)題
    private final String title = "xxx接口";
    // 具體的描述
    private final String description = "公共數(shù)據(jù)服務(wù)接口文檔";
    // basePackage
    private final String basePackage = "com.snnu.mbts.controller";
    
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .version(version)
                .build();
    }
}

第三步:在Controller類中設(shè)置接口信息

比如接口名稱、接口信息,接口參數(shù)

@ApiOperation(value = "用戶登陸請(qǐng)求", notes = "注意事項(xiàng)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginName", value = "用戶名", required = true, paramType = "query", example = "張三"),
            @ApiImplicitParam(name = "password", value = "密碼", required = true, paramType = "query", example = "111111")
    })
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容