springboot knife4j 之api接口文档和调试工具

1.添加依赖

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.github.xiaoymin</groupId>
                <artifactId>knife4j-dependencies</artifactId>
                <version>2.0.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
 <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
        </dependency>

2.创建配置类

package com.example.knife4j.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
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
@EnableKnife4j
public class SwaggerConfiguration {


    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //分组名称
                .groupName("2.X版本")
                .select()
                //这里指定Controller扫描包路径(项目路径也行)
                .apis(RequestHandlerSelectors.basePackage("com.example.knife4j.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("标题标题")
                .description("api接口描述")
                .termsOfServiceUrl("http://localhost:88888/")
                .contact("1123@qq.com")
                .version("1.0")
                .build();
    }
}

3.创建实体bean

package com.example.knife4j.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
 * Created by lwq 2020/8/14.
 */


@ApiModel(value = "用户模型")
public class UserEntity {
    @ApiModelProperty(value="id" ,required= true,example = "1")
    private Integer id;
    @ApiModelProperty(value="用户姓名" ,required=true,example = "张三")
    private String name;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "DemoDoctor [id=" + id + ", name=" + name + "]";
    }

}

4.测试类

package com.example.knife4j.controller;

import com.example.knife4j.entity.UserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@Api(value = "用户接口")
@RestController
public class UserController {


    @ApiOperation(value = "获取用户信息接口", nickname = "根据用户ID获取用户相关信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int")
    @GetMapping("/getUser")
    public UserEntity getUser(@RequestParam Integer id) {
        UserEntity userEntity = new UserEntity();
        userEntity.setId(id);
        userEntity.setName("管理员");
        return userEntity;
    }


    @ApiOperation(value = "添加用户", nickname = "添加用户接口1", notes = "入参是复杂对象", produces = "application/json")
    @PostMapping("/postUser")
    @ResponseBody
    @ApiImplicitParam(paramType = "query", name = "userId", value = "用户id", required = true, dataType = "int")
    public UserEntity postUser(@RequestBody UserEntity user, @RequestParam("userId") int userId) { // 这里用包装类竟然报错
        if (user.getId() == userId) {
            return user;
        }
        return new UserEntity();
    }


    @ApiOperation(value = "添加用户", nickname = "添加用户接口2", notes = "入参是简单对象", produces = "application/json")
    @PostMapping("/addUser")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userName", value = "用户姓名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "用户id", required = true, dataType = "int")})
    public UserEntity addUser(String userName, int id) {
        UserEntity userEntity = new UserEntity();
        userEntity.setName(userName);
        userEntity.setId(id);
        return userEntity;
    }

}

5.文档接口 http://localhost:8080/doc.html

6.如果不想在生产环境被访问到,可以增加如下配置

#为了接口安全,需要屏蔽所有Swagger的相关资源,生产环境设置为true
knife4j.production=true

 

### 回答1: Spring Boot Knife4j Demo 是一个使用 Spring Boot 和 Knife4j 技术实现的演示项目。 Spring Boot 是一个基于 Spring 框架的开发框架,它简化了 Spring 应用的开发和部署。它提供了诸多特性,比如自动配置、快速开发和易于扩展等,使得开发人员能够更加专注于业务逻辑的实现。在 Spring Boot 中集成了 Knife4j 技术能够方便地生成和维护接口文档Knife4j 是一个基于 Swagger 的接口文档生成工具,它提供了一种便捷的方式来生成和管理接口文档Knife4j 能够自动生成接口文档,并且通过可视化界面提供了接口测试和调试的功能。它的特点包括丰富的注解支持、易于扩展和美观的界面设计等。 Spring Boot Knife4j Demo 是一个以 Spring Boot 为基础,使用 Knife4j 技术生成接口文档的示例项目。这个演示项目展示了如何使用 Spring Boot 和 Knife4j 结合开发一个简单的接口文档服务。项目通过注解方式定义接口和参数,并通过配置 Knife4j 实现自动生成接口文档。通过访问 Knife4j 提供的界面,我们能够查看和测试接口的功能。 Spring Boot Knife4j Demo 的实现过程相对简单,配置逻辑清晰。它能够帮助开发人员快速上手 Knife4j 技术,并在实际项目中方便地生成和维护接口文档。同时,通过演示项目,我们也能更好地理解 Spring Boot 和 Knife4j 的使用方法,加深对这两种技术的理解和应用。 ### 回答2: Spring Boot Knife4j demo是一个使用Spring Boot框架和Knife4j插件开发的示例项目。Spring Boot是一个快速开发Java应用程序的框架,它提供了自动配置和起步依赖项,使开发人员能够快速搭建和运行Java应用程序。Knife4j是一个基于Swagger的API文档工具,它可以帮助开发人员生成和展示API文档。 该demo项目使用了Spring Boot框架,通过自动配置和依赖管理,开发人员可以轻松地创建和部署Java应用程序。同时,该项目还集成了Knife4j插件,以便生成和展示API文档。通过Knife4j,开发人员可以浏览和测试API接口,查看接口的详细信息和参数说明。 在该demo项目中,开发人员可以通过配置Swagger注解来定义API接口,并添加相关的API文档说明。通过访问项目的Swagger UI页面,可以查看文档中定义的所有API接口,并可以直接在页面上进行测试和调试。 除了基本的API文档生成和展示功能外,该demo项目还提供了一些其他特性。例如,可以配置权限控制,只允许特定的用户或角色访问API文档;还可以配置接口文档的自定义主题,以使文档更加美观和易于阅读。 总之,Spring Boot Knife4j demo是一个演示如何使用Spring Boot框架和Knife4j插件来开发和展示API文档的示例项目。它提供了简单易用的API文档生成和展示功能,并支持更多的扩展和配置选项,方便开发人员根据自己的需求进行定制和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值