【SpringBoot专题_01】springboot集成Knife4J

简介: 【SpringBoot专题_01】springboot集成Knife4J

前言

技术栈:springboot、Knife4j

gitee源码地址https://gitee.com/shawsongyue/sevenhee_modules/tree/master/springboot_knife4jSwagger

1.基础maven依赖

<!--(新版本)swagger增强工具依赖包,方便生成接口文档。非必须导入-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

2.目录结构与配置文件

application.yml

server:
  port: 8080

3.创建knife4j配置文件(Knife4SwaggerConfig.class)

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
 * knife4版本的swagger配置
 *
 * @author xiaosongyue
 * @date 2021/04/16 11:48:40
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class Knife4SwaggerConfig {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xsy.sevenhee.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot测试项目")
                .description("项目描述,冲冲冲")
                .version("1.0.0")
                .contact(new Contact("xiaosongyue","http://www.baidu.com","1035336407@qq.com"))
                .license("Home")
                .licenseUrl("http://localhost:8080/swagger-ui.html")
                .build();
    }
}

4.创建controller层(SwaggerController.class)

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
/**
 * swagger控制器
 *
 * @author xiaosongyue
 * @date 2021/04/16 11:26:39
 */
@RestController
@RequestMapping("/swagger")
@Api(value = "swagger模块",tags = "swaggerController")
public class SwaggerController {
    @GetMapping("/add")
    @ApiOperation(value = "增加",notes = "增加信息")
    public String add(String username,String password){
        System.out.println("账号:"+username);
        System.out.println("密码:"+password);
        return "增加成功";
    }
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除",notes = "删除信息")
    public String delete(String id){
        System.out.println("要删除的id:"+id);
        return "删除成功";
    }
    @PostMapping("/update")
    @ApiOperation(value = "修改",notes = "修改信息")
    public String update(String id,String context){
        System.out.println("要删除的id:"+id);
        System.out.println("要删除的内容:"+context);
        return "修改成功";
    }
    @GetMapping("/select")
    @ApiOperation(value = "查询",notes = "查询信息")
    public String select(){
        System.out.println("开始查询");
        return "查询成功";
    }
}

4.创建主启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * knife4版本的swagger的应用程序
 *
 * @author xiaosongyue
 * @date 2021/04/16 11:47:56
 */
@SpringBootApplication
public class Knife4SwaggerApplication {
    public static void main(String[] args) {
        SpringApplication.run(Knife4SwaggerApplication.class,args);
    }
}

5.访问测试

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

6.gitee源码地址

访问地址:https://gitee.com/shawsongyue/sevenhee_modules/tree/master/springboot_knife4jSwagger

7.若有问题,欢迎交流

相关文章
|
10月前
|
JSON 分布式计算 大数据
springboot项目集成大数据第三方dolphinscheduler调度器
springboot项目集成大数据第三方dolphinscheduler调度器
688 3
|
分布式计算 大数据 Java
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
284 0
|
10月前
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
890 0
第07课:Spring Boot集成Thymeleaf模板引擎
|
10月前
|
存储 人工智能 Java
Springboot集成AI Springboot3 集成阿里云百炼大模型CosyVoice2 实现Ai克隆语音(未持久化存储)
本项目基于Spring Boot 3.5.3与Java 17,集成阿里云百炼大模型CosyVoice2实现音色克隆与语音合成。内容涵盖项目搭建、音色创建、音频合成、音色管理等功能,适用于希望快速掌握Spring Boot集成语音AI技术的开发者。需提前注册阿里云并获取API Key。
|
10月前
|
Java 关系型数据库 MySQL
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
998 2
|
10月前
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
538 2
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 项目管理
springboot项目集成dolphinscheduler调度器 项目管理
314 0
|
11月前
|
前端开发
SpringBoot2.3.1集成Knife4j接口文档
SpringBoot2.3.1集成Knife4j接口文档
1158 44
|
11月前
|
缓存 安全 Java
Shiro简介及SpringBoot集成Shiro(狂神说视频简易版)
Shiro简介及SpringBoot集成Shiro(狂神说视频简易版)
901 7

热门文章

最新文章