knife4j 使用说明

一、创建配置类(标红处为扫描路径)

package com.zjy.springboot.myconfig;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {
    @Bean(value = "dockerBean")
    public Docket dockerBean() {
        //指定使用Swagger2规范
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //描述字段支持Markdown语法
                        .description("# Knife4j RESTful APIs")
                        .termsOfServiceUrl("https://doc.xiaominfo.com/")
                        .contact("jiyou.zhang@aliyun.com")
                        .version("1.0")
                        .build())
                //分组名称
                .groupName("你好")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage(" com.zjy.springboot.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
}

二、pom.xml 加入依赖

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
    <version>4.0.0</version>
</dependency>

 

三、关于实体类的标注 

package com.zjy.springboot.vo;

import io.swagger.annotations.ApiModelProperty;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import lombok.ToString;

import lombok.experimental.Accessors;

@Data

@ToString

@AllArgsConstructor

@NoArgsConstructor

@Accessors

public class Student {

    @ApiModelProperty("sid")

    private String sid;

    @ApiModelProperty("学生姓名")

    private String stuName;

    @ApiModelProperty("学生年龄")

    private Integer stuAge;

    @ApiModelProperty("学生地址")

    private String stuAddress;

}

四、controller(接口)

 

package com.zjy.springboot.controller;

import com.zjy.springboot.vo.Student;

import io.swagger.annotations.*;

import org.springframework.validation.annotation.Validated;

import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

import java.util.List;

@Api(tags ="学生管理")

@RestController

public class KnifeController {

    @ApiOperation(value = "新增学生") //该方法的说明

    @RequestMapping(value = "/stu",method = RequestMethod.POST)

    public String insert (@RequestBody  Student student) { //@validated 检测数据是否正确

        System.out.println(student.toString());

        return student.toString();

    }

    @ApiOperation(value = "删除学生")

    @RequestMapping(value = "/stu/{sid}", method = RequestMethod.DELETE)

    public boolean del(@PathVariable("sid") String sid) {

        return true;

    }

    @ApiOperation(value = "查询学生")  //方法的说明

    @ApiImplicitParams({

            @ApiImplicitParam(paramType = "query",name = "sid",example = "101",dataTypeClass =Student.class ),

            @ApiImplicitParam(paramType = "query",name = "sName",example = "张三",dataTypeClass =Student.class ),

            @ApiImplicitParam(paramType = "query",name = "sAge",example = "22",dataTypeClass =Student.class ),

            @ApiImplicitParam(paramType = "query",name = "sAddress",example = "山西太原",dataTypeClass =Student.class ),

    }) // 参数的说明

    @ApiResponses({

            @ApiResponse(code = 200,message = "成功"),

            @ApiResponse(code = 403,message = "权限不足"),

            @ApiResponse(code = 404,message = "资源不存在"),

            @ApiResponse(code = 500,message = "方法异常"),

    })

    @RequestMapping(value = "/student",method = RequestMethod.GET)

    public List<Student> students(@RequestParam(value = "sid",required = true)String sid,

                                  @RequestParam(value = "sName",required = false) String name,

                                  @RequestParam("sAge") Integer age,

                                  @RequestParam("sAddress")String address) {

        List<Student> students = new ArrayList<>();

        students.add(new Student("1", "张三", 23, "山西太原"));

        students.add(new Student("2", "李四", 23, "山西太原"));

        return students;

    }

    @ApiOperation(value = "修改学生")

    @RequestMapping(value = "/stu",method = RequestMethod.PUT)

    public int update(@RequestBody Student student) {

        return 1;

    }

}

 访问地址:localhost:8080/doc.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值