您好,登錄后才能下訂單哦!
本篇內容主要講解“怎么配置swagger”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么配置swagger”吧!
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,默認只在本地環境開啟 * */ @EnableSwagger2 @Configuration public class Swagger2Config { /** * 是否開啟swagger */ @Value(value = "${swagger.enabled}") Boolean swaggerEnabled; /** * 掃描對應的包路徑,生成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("/"); } /** * 設置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 = "每頁數量", 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”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。