SpringBoot 整合Swagger2

簡(jiǎn)介

現(xiàn)在大家開(kāi)發(fā)基本用的都是Restful風(fēng)格,而它的搭檔Swagger也就不用多說(shuō)了,不僅能幫我們?cè)诰€(xiàn)快速生成接口文檔,還能進(jìn)行接口功能測(cè)試,本片文章總結(jié)下怎么能快速將Swagger2整合到自己的項(xiàng)目中,話(huà)不多說(shuō),一個(gè)字:。

Maven Pom.xml 引入

 <!-- 與swagger一起使用,需要注意-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>

Swagger 配置文件

package com.glj.member.produce.config;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${swagger2.enable}")
    private boolean enable;//是否開(kāi)啟swaager,如果生產(chǎn)環(huán)境,則禁止

    //如果不需要進(jìn)行模塊區(qū)分,直接用默認(rèn)模塊即可
    @Bean("默認(rèn)模塊")
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("默認(rèn)模塊")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.glj.member.produce"))//Swagger Api掃描包路徑
                .paths(PathSelectors.any())
                .build();
    }

    @Bean("用戶(hù)模塊")
    public Docket createUserApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("用戶(hù)模塊")
                .select()
                //.apis(RequestHandlerSelectors.basePackage("com.glj.member.produce"))//Swagger Api掃描包路徑
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex("/user.*"))
                .build()
                .apiInfo(apiInfo())
                .enable(enable);//是否開(kāi)啟
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot 整合Swagger")//Ui頁(yè)面Title
                .description("KXL oauth2-sso-client-member-produce  API") //說(shuō)明
                .termsOfServiceUrl("http://www.itdecent.cn/nb/35744583")//自己或者單位的官方服務(wù)地址
                .version("2.0")//版本
                .build();
    }

}

Controller 類(lèi)

package com.glj.member.produce.oauth2.controller;

import com.glj.member.produce.oauth2.service.ISysUserService;
import com.glj.model.entity.SysUserPo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author gaoleijie
 * @Date 2019/4/15 20:08
 **/
@RestController
@RequestMapping("/user")
@Api(tags = "用戶(hù)管理")
public class SysUserController {
    @Autowired
    private ISysUserService userService;

    @PostMapping(value = "/getUserByUserCode", consumes = MediaType.APPLICATION_JSON_VALUE)
    public SysUserPo getUserByUserCode(@ApiParam("userCode") String userCode) {
        return userService.getUserByUserCode(userCode);
    }
}

效果圖

效果圖

結(jié)束語(yǔ)

就這么簡(jiǎn)單,這些都是自己經(jīng)常使用的,也夠用,也沒(méi)有太深研究,如果總結(jié)不到位或者漏掉的部分,歡迎評(píng)論指正。

?著作權(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)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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