SpringBoot2.x学习-Knife4j集成 - Go语言中文社区

SpringBoot2.x学习-Knife4j集成


一、Knife4j介绍

1.1 官方地址

https://doc.xiaominfo.com/

1.2官方说明介绍

knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案,前身是swagger-bootstrap-ui,取名kni4j是希望她能像一把匕首一样小巧,轻量,并且功能强悍!

二、spring boot2集成Knife4j

2.1添加Knife4j依赖

jdk 1.8
spring boot 2.3.8.RELEASE

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

2.2编写Knife4j配置文件

Knife4jConfig.java

@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {

    @Bean
    public Docket createDocket() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("FireApiDoc")
                        .description("Fire Application RESTful APIs")
                        .version("1.0")
                        .contact(new Contact("David", "http://www.huowawa.com", "hello@qq.com"))
                        .build())
                .select()
                //这里指定Controller扫描包路径
                //.apis(RequestHandlerSelectors.any())
                //.apis(RequestHandlerSelectors.basePackage("com.soft.fire.controller"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build()
                .directModelSubstitute(Timestamp.class, String.class);

        return docket;
    }
}

2.3 编写业务代码

@RestController
@RequestMapping("/emp")
@Api(tags = "员工API")
public class EmpController {
    /**
     * 日志操作对象
     */
    private Log logger = LogFactory.get();
    /**
     * 注入service
     */
    @Resource
    private EmpService empService;

    @GetMapping
    @ApiOperation("查询所有员工数据")
    public List<Emp> all() {
        return empService.list();
    }

    @PostMapping
    @ApiOperation(value = "新增或修改员工信息", notes = "如果是修改员工信息则ID必填")
    public String emp(@RequestBody  EmpDTO empDTO) {
        empService.save(empDTO);
        return "success";
    }

@Data
@TableName("emp")
@ApiModel(value = "员工实体对象")
public class Emp implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 主键ID
     */
    @ApiModelProperty(value = "主键ID")
    private String id;

    /**
     * 雇员的编号
     */
    @ApiModelProperty(value = "雇员的编号",required = true)
    private Integer empno;

    /**
     * 雇员的姓名
     */
    @ApiModelProperty(value = "雇员的姓名",required = true)
    private String ename;

    /**
     * 雇员的职位
     */
    @ApiModelProperty(value = "雇员的职位",required = true)
    private String job;

    /**
     * 雇员对应的领导编号,领导也是雇员
     */
    @ApiModelProperty(value = "雇员对应的领导编号",required = true)
    private String mgr;

    /**
     * 入职日期
     */
    @ApiModelProperty(value = "入职日期",required = true)
    private Date hiredate;

    /**
     * 工资
     */
    @ApiModelProperty(value = "工资",required = true)
    private Double sal;

    /**
     * 奖金
     */
    @ApiModelProperty(value = "奖金")
    private Double comm;

    /**
     * 雇员所在的部门编号
     */
    @ApiModelProperty(value = "雇员所在的部门编号",required = true)
    private Integer deptno;

    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    @ApiModelProperty(value = "创建时间",hidden = true)
    private Timestamp createTime;

    /**
     * 更新时间
     */
    @TableField(fill = FieldFill.UPDATE)
    @ApiModelProperty(value = "更新时间",hidden = true)
    private Timestamp updateTime;


}

2.4 访问接口文档

启动spring boot应用,在浏览器输入http://localhost:8080/doc.html,结果如下:
在这里插入图片描述

2.5 注意事项

如果项目重写了ResponseBodyAdvice做全局响应体处理,则需要把Swagger的响应数据排除掉,否则接口文档不会显示出来。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/linjinhuo/article/details/115566589
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢