本篇内容主要讲解“怎么配置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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。