springboot項目引入Swagger2步驟

第一步:增長jar包,引入最新版本2.9.2,訪問的時候會報錯java.lang.NumberFormatException,排除報錯的包能夠解決、也能夠引入舊的jar包html

<!-- 添加Swagger2依賴,用於生成接口文檔 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
            <!-- 報錯信息:java.lang.NumberFormatException: For input string: 「」 排除jar包引用舊的jar包解決問題 -->
            <exclusions>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-annotations</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-models</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- 引用舊的jar 排除新jar包中的問題 start -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.21</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.21</version>
        </dependency>

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

引入舊jar包java

<!-- 添加Swagger2依賴,用於生成接口文檔 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.7.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.7.0</version>
    </dependency>
    <!--end-->

第二步:配置Configuration配置spring

@Configuration
@EnableSwagger2
public class Swagger2Configuration {
    //api接口包掃描路徑
    public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.cn.auth.controller.census";
    public static final String VERSION = "1.0.0";

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
                .paths(PathSelectors.any()) // 能夠根據url路徑設置哪些請求加入文檔,忽略哪些請求
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("權限管理系統") //設置文檔的標題
                .description("權限管理系統API接口文檔") //設置文檔的描述
                .version(VERSION) //設置文檔的版本信息
                .termsOfServiceUrl("localhost:8088") // 設置文檔的License信息
                .build();
    }
}

第三步:配置shiro權限過濾數據庫

filterChainDefinitionMap.put("/swagger-ui.html","anon");
 filterChainDefinitionMap.put("/v2/*","anon");
 filterChainDefinitionMap.put("/swagger-resources/**","anon");

第四步:方法描述示例api

@Api(description = "示例文檔API接口")
@RestController
@RequestMapping("/swagger_example")
public class SwaggerExample {
    @RequestMapping(value ="getTableList",method=RequestMethod.POST)
    @ApiOperation(value="獲取數據庫表的列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="tableName",value="數據庫表名",required=false,paramType="form",dataType="String"),
            @ApiImplicitParam(name="page",value="第幾頁",required=true,paramType="form",dataType="int"),
            @ApiImplicitParam(name="floatExp",value="傳float類型數據",required=false,paramType="form",dataType="float"),
            @ApiImplicitParam(name="doubleExp",value="傳double類型數據",required=false,paramType="form",dataType="double"),
            @ApiImplicitParam(name="booleanExp",value="傳boolean類型數據",required=false,paramType="form",dataType="boolean"),
            @ApiImplicitParam(name="limit",value="每頁多少條數據",required=true,paramType="form",dataType="int")})
 public BasePageData getTableList(String tableName, Integer page, Integer limit) {
        BasePageData data = new BasePageData();
        data.setMsg("操做成功");
        return data;
    }
}

第四步:請求地址出現接口頁面
http://localhost:8088/swagger-ui.html
app

相關文章
相關標籤/搜索