Springboot集成swagger

Springboot集成swagger

一、導(dǎo)入swagger所需maven依賴包

        <!---swagger-ui-->
        <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>

二、wagger配置類

目錄結(jié)構(gòu)配置如下


wagger 配置目錄.png

wagger.java 代碼如下

特別要注意的是里面配置了api文件也就是controller包的路徑,不然生成的文檔掃描不到接口。
@Configuration
public class Swagger2 {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.volunteer.sys.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot利用swagger構(gòu)建api文檔")
                .description("簡(jiǎn)單優(yōu)雅的restfun風(fēng)格,http://blog.csdn.net/saytime")
                .version("1.0")
                .build();
    }
}

三、Application.class 加上注解@EnableSwagger2 表示開啟Swagger

代碼如下

@Controller
@SpringBootApplication
@EnableSwagger2
@MapperScan(basePackages = "com.volunteer.sys.dao")
public class VolunteerHelpApplication {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(VolunteerHelpApplication.class, args);
    }

}

四、測(cè)試

在瀏覽器打開http://localhost:8080/swagger-ui.html#/

如圖,配置成功


swagger配置成功頁(yè)面.png
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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