在SpringBoot中使用Swagger

  1. 首先需要导入Swagger依赖
    <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency>
    <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency>

  2. 在使用Swagger的Controller中使用注解@EnableSwagger2

  3. 定义Swagger的Bean

@Bean
public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.elong.Controller")) .paths(PathSelectors.ant("/api/*")) .build().apiInfo(apiInfo()); }

private ApiInfo apiInfo() { ApiInfo apiInfo = new ApiInfo( "Autops REST API", "Autops平台接口", "API TOS", "OPS", "xianghui.niu", "License of API", ""); return apiInfo; }

  1. 主要的注解使用方法
  • @Api : 注解Controller,对应UI中一组API 的命名 description: 描述信息, tag: 标签
  • @ApiOperation value: api的功能介绍
  • @ApiImplicitParam(name = "id", value = "执行单号", paramType = "query")
  • @ApiResponses(value = {@ApiResponse(code = 200, message = "接口调用成功")})
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容