Swagger2+Apizza接口文档

Swagger2的使用
//  这个是基于Springboot环境
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

Swagger2接口文档说明

定义swagger配置类 放到启动类能扫到的地方
package com.itheima.health.config;

import com.itheima.health.ConsumerMain;
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;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        String basePck = WebApplicaiton.class.getPackage().getName();
        System.out.println(basePck+"=启动类所在的包为路径扫描=====定义生成接口文档的包路径=====");
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePck))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("传智播客_传智健康_接口文档")
                .description("描述内容")
                .version("2.1.1")
                .build();
    }

}
启动类
package com.itheima;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2  //  开启swagger配置
public class WebApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class,args);
        System.out.println("===health_web消费端启动---");
    }
}
Swagger常用注解说明

在这里插入图片描述
常用两个注解: 一个类 和 一个方法

@Api 和 @ApiOperation
示例: controller包下开发检查项所有列表查询

//第一种无参数的情况下

//   这个是无参的
@RestController
@Api(tags = "检查项模块管理")
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;
      //   开发 检查项模块   crud
    @GetMapping("checkitem/findAll")
    @ApiOperation(value = "查询检查项方法",notes = "查询所有的检查项列表信息")
    public Result findAll(){
            List<CheckItem> list = checkItemService.list();
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
    }
}

//第二种有参数的情况下

@ApiOperation(value = "套餐查询",notes = "分页套餐查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="currentPage",value="当前分页页码",required=true,paramType="form",dataType="Integer"),
            @ApiImplicitParam(name="pageSize",value="每页显示记录数",required=true,paramType="form",dataType="Integer"),
            @ApiImplicitParam(name="queryString",value="查询条件",required=false,paramType="form",dataType="String")
    })
    @ApiResponses({
            @ApiResponse(code=400,message="请求参数没填好"),
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })

   //  上述注解 添加在目标方法上! 
   public Result findPage(@RequestBody QueryPageBean queryPageBean){
            PageResult pageResult = setmealService.findPage(queryPageBean);
            return  new Result(pageResult);
    }

访问页面: 查询文档接口
去这个网页上能下载
http://localhost:8081/swagger-ui.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Swagger 是一个用于构建、文档化和使用 RESTful Web 服务的开源工具。Swagger 有很多版本,其中 Swagger2 是其中最常用的一个版本。Swagger2 可以通过注解的方式生成 API 接口文档,这些注解包括 @Api、@ApiOperation、@ApiParam 等。 下面是使用 Swagger2 生成 API 接口文档的步骤: 1. 添加 Swagger2 依赖 在项目的 pom.xml 文件中添加 Swagger2 的依赖: ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> ``` 2. 配置 Swagger2 在 Spring Boot 应用的启动类上添加 @EnableSwagger2 注解开启 Swagger2 支持,并配置 Docket 对象: ``` @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } } ``` 这个配置会扫描所有的 Controller 类,并生成 API 接口文档。 3. 添加 Swagger2 注解 在 Controller 类的方法上添加 Swagger2 注解,包括: - @Api:用于标识这个 Controller 类的作用和说明。 - @ApiOperation:用于标识这个方法的作用和说明。 - @ApiParam:用于标识方法参数的作用和说明。 示例代码: ``` @RestController @RequestMapping("/api") @Api(value = "HelloWorldController", description = "示例控制器") public class HelloWorldController { @GetMapping("/hello") @ApiOperation(value = "打招呼", notes = "向用户打招呼") public String hello(@ApiParam(name = "name", value = "用户名", required = true) @RequestParam String name) { return "Hello, " + name + "!"; } } ``` 4. 访问 Swagger UI 启动应用后,访问 http://localhost:8080/swagger-ui.html 可以看到 Swagger UI 界面,其中包含了生成的 API 接口文档。在这个界面中,可以查看 API 接口的详细信息、测试 API 接口等。 以上就是使用 Swagger2 生成 API 接口文档的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值