本篇內(nèi)容主要講解“怎么配置swagger”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“怎么配置swagger”吧!
創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供東河網(wǎng)站建設(shè)、東河做網(wǎng)站、東河網(wǎng)站設(shè)計、東河網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、東河企業(yè)網(wǎng)站模板建站服務(wù),十余年東河做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
maven依賴:
io.springfox springfox-swagger2 2.8.0 io.springfox springfox-swagger-ui 2.8.0
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,默認只在本地環(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”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!