怎么配置swagger

本篇內(nèi)容主要講解“怎么配置swagger”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么配置swagger”吧!

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供東河網(wǎng)站建設(shè)、東河做網(wǎng)站、東河網(wǎng)站設(shè)計(jì)、東河網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、東河企業(yè)網(wǎng)站模板建站服務(wù),十余年東河做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

maven依賴:

<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配置類

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;

/**
 * 配置Swagger,用于測試rest,默認(rèn)只在本地環(huán)境開啟
 *
 */
@EnableSwagger2
@Configuration
public class Swagger2Config {

    /**
     * 是否開啟swagger
     */
    @Value(value = "${swagger.enabled}")
    Boolean swaggerEnabled;

    /**
     * 掃描對應(yīng)的包路徑,生成API
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                // 是否開啟
                .enable(swaggerEnabled).select()
                // 要掃描的包
                .apis(RequestHandlerSelectors.basePackage("cn.com.test.channel.controller"))
                .paths(PathSelectors.any()).build().pathMapping("/");
    }

    /**
     * 設(shè)置API 信息
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxx")
                .description("xxx")
                .version("1.0.0")
                .build();
    }
}

常用注解:

  • Api

  • ApiModel

  • ApiModelProperty

  • ApiOperation

  • ApiParam

  • ApiResponse

  • ApiResponses

  • ResponseHeader

Ex:

@Data
@ApiModel
public class UserRepay {

    @NotNull(message = "用戶ID不能為空")
    @ApiModelProperty("用戶ID")
    private Long userId;
}

@RestController
@Api(tags = "還款管理", description = "還款管理API")
@RequestMapping("repay")
public class UserRepayController extends BaseController {

    @Resource
    private UserRepayService userRepayService;

    @ApiOperation(value = "獲取還款列表", notes = "獲取還款列表 userId用戶ID必填")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每頁數(shù)量", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "pageNumber", value = "頁碼", dataType = "Long", paramType = "query")
    })
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    public Object get(Long pageNumber, Long pageSize,
                      @RequestBody @ApiParam(value = "還款") UserRepay userRepay) {
        return ResponseUtil.getSuccessMap(userRepayService.get(pageNumber, pageSize, userRepay));
    }
}

到此,相信大家對“怎么配置swagger”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

文章標(biāo)題:怎么配置swagger
標(biāo)題鏈接:http://muchs.cn/article24/jpecce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)外貿(mào)網(wǎng)站建設(shè)、微信小程序、動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司