springboot整合springfox-swagger2

引入pom

<!--springboot使用的是2.1.4.RELEASE版本 -->
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
    <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>
</dependencies>

配置swagger配置類

SwaggerConfig.class

package com.lee.room.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ApiInfoBuilder;
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;
import java.util.ArrayList;
import java.util.function.Predicate;

import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;

/**
 * @Author li.heng
 * @Date 2019/6/25 17
 * @Description: EnableSwagger2注解 開啟Swagger支持
 **/
@EnableSwagger2
@Configuration
@ComponentScan("com.lee.room.controller.*")
public class SwaggerConfig {


    @Bean
    public Docket swaggerCoreConfig() {
        // 構(gòu)造函數(shù)傳入初始化規(guī)范,這是swagger2規(guī)范
        return new Docket(DocumentationType.SWAGGER_2)
                // 添加api詳情信息
                .apiInfo(getInfo())
                // 添加默認(rèn)參數(shù)列表
                .globalOperationParameters(new ArrayList<>())
                .select()
                // 添加過濾條件,
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 這里是控制哪些路徑的api會(huì)被顯示出來,比如下方的參數(shù)就是除了/user以外的其它路徑都會(huì)生成api文檔
                .paths((String a) ->
                        !a.equals("/user"))
                .build();
    }

    private ApiInfo getInfo() {
        // contact為聯(lián)系信息對象,在訪問接口頁面時(shí)會(huì)呈現(xiàn),參數(shù)一為名稱,參數(shù)二為聯(lián)系url,參數(shù)三為
        return new ApiInfoBuilder()
                .contact(
                        new Contact("小籠包",
                                "https://github.com/lhdhr5828",
                                "873093067@qq.com"))
                .title("room api")
                .description("房產(chǎn)信息project")
                .termsOfServiceUrl("https://www.baidu.com")
                .extensions(new ArrayList<>(16))
                .license("許可信息")
                .version("1.0").build();
    }
}

新建Controller

IndexController.class

package com.lee.room.controller;


import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author heng.li
 * @Date 2019/4/11 13
 * @Description
 **/
@RestController
public class IndexController {

    /**
     * @return
     */
    @ApiOperation(value = "index接口")
    @GetMapping(value = "/")
    public String index() {
        return "This is room project";
    }

    /**
     * tags 類似于group,下面會(huì)有一系列的接口
     * httpMethod是指接口頁面上顯示的請求方法
     * @param str
     * @return
     */
    @ApiOperation(value = "測試有參數(shù)接口", notes = "note", tags = "tags", httpMethod = "POST", protocols = "protocols")
    @PostMapping(value = "/get/str")
    public String getInfo(String str) {
        return "test param";
    }
}

application.properties

server.port=8080

啟動(dòng)項(xiàng)目

訪問http://localhost:8080/swagger-ui.html

api文檔主頁面

點(diǎn)擊接口還可以進(jìn)行請求測試

單個(gè)接口

測試結(jié)果:

測試結(jié)果
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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