swagger简单使用

1.基本使用

springboot版本

  1. 导包

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>
    
  2. 接口使用注解

    @RestController
    @RequestMapping("/stu")
    @Api(tags = "学生信息管理")
    public class StuController {
        @Autowired
        private StuService stuService;
    
        /**
         * 添加
         * @param stuInfoDto
         * @return
         */
        @PostMapping
        @ApiOperation(value = "插入一条信息")
        public PageResult save(@RequestBody StuInfoDto stuInfoDto) {
            System.out.println("stu save...");
            return stuService.save(stuInfoDto);
        }
    
        /**
         * 删除
         * @param sid
         * @return
         */
        @DeleteMapping("{sid}")
        @ApiOperation(value = "删除一条信息")
        public PageResult delete(@PathVariable Integer sid) {
            System.out.println("stu delete..." + sid);
            return stuService.delete(sid);
        }
    
        /**
         * 更新
         * @param stuInfoDto
         * @return
         */
        @PutMapping
        @ApiOperation(value = "更新一条信息")
        public PageResult update(@RequestBody StuInfoDto stuInfoDto) {
            System.out.println("stu update...");
            return stuService.update(stuInfoDto);
        }
    
        /**
         * 根据sid获取
         * @param sid
         * @return
         */
        @GetMapping("{sid}")
        @ApiOperation(value = "根据sid查找信息")
        public PageResult getBySid(@PathVariable Integer sid) {
            System.out.println("stu getByUserCode..." + sid);
            return stuService.getBySid(sid);
        }
    
        /**
         * 获取全部
         * @return
         */
        @GetMapping
        @ApiOperation(value = "查询所有信息")
        public PageResult getAll() {
            System.out.println("stu getAll...");
            return stuService.getAll();
        }
    }
    
  3. 配置swagger

    @Configuration
    @EnableOpenApi
    public class SwaggerConfig {
        @Bean
        public Docket docket(){
            return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo()).enable(true)
                .select()
                //添加swagger接口提取范围,修改成指向你的controller包
                .apis(RequestHandlerSelectors.basePackage("com.rokned.mypro.controller"))
                .paths(PathSelectors.any())
                .build();
        }
    
        private ApiInfo apiInfo(){
            return new ApiInfoBuilder()
                .title("学生信息接口文档")
                .description("学生信息查询系统")
                .version("1.0")
                .build();
        }
    }
    
  4. 访问连接http://localhost:8999/swagger-ui/index.html即可

如果报错

在这里插入图片描述

可能是因为springboot版本高,替换了路径匹配,修改路径匹配配置即可

spring:
 mvc:
  pathmatch:
   matching-strategy: ant_path_matcher

2.kinfe4j框架

使用swagger3

  1. 导入依赖

    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>knife4j-spring-boot-starter</artifactId>
        <version>3.0.3</version>
    </dependency>
    
  2. 配置(可选)

    @Configuration
    @EnableOpenApi
    // @EnableKnife4j
    // @EnableSwagger2
    public class Knife4jConfig {
        @Bean
        public Docket docket() {
            Docket docket = new Docket(DocumentationType.OAS_30)
                    .apiInfo(new ApiInfoBuilder()
                            .title("我的标题")
                            .description("我的描述")
                            // .termsOfServiceUrl("http://www.xx.com/")
                            .contact(new Contact("knife", "https://knife.blog.csdn.net/", "xx@qq.com"))
                            .version("1.0")
                            .build())
                    // 分组名称
                    .groupName("all")
                    .select()
                    // 这里指定Controller扫描包路径
                    .apis(RequestHandlerSelectors.basePackage("com.example.demo"))
                    .paths(PathSelectors.any())
                    .build();
     
            return docket;
        }
    }
    
  3. 设置注解

    //实体属性:
    @ApiModelProperty(value = "学生主键",required = true)
    private Integer sid;
    
    @ApiModelProperty("籍贯")
    private String nativePlace;
    
    
    //controller
    @Api(tags = "学生信息管理")
    public class StuController {
        @ApiOperation(value = "插入一条信息")
        public PageResult save(@RequestBody StuInfoDto stuInfoDto) {
            System.out.println("stu save...");
            return stuService.save(stuInfoDto);
        }
    }
    
  4. 启动服务访问http://localhost:8999/doc.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值