程序员子龙(Java面试 + Java学习) 程序员子龙(Java面试 + Java学习)
首页
学习指南
工具
开源项目
技术书籍

程序员子龙

Java 开发从业者
首页
学习指南
工具
开源项目
技术书籍
  • 基础

  • JVM

  • Spring

  • 并发编程

  • Mybatis

  • 网络编程

  • 数据库

  • 缓存

  • 设计模式

  • 分布式

  • 高并发

  • SpringBoot

    • SpringBoot 整合redis
    • SpringBoot 线程池
    • springboot下整合mybatis
    • spring boot 配置文件的加载顺序
    • springboot启动不加载bootstrap.yml文件的问题解决
    • SpringBoot设置动态定时任务
    • springboot整合hibernate
    • ApplicationRunner、InitializingBean、@PostConstruct使用详解
    • Spring Boot 优雅的参数校验方案
    • ELK处理 SpringBoot 日志,太优雅了!
    • SpringBoot配置数据源
    • Spring Boot 默认数据库连接池 —— HikariCP
    • 数据库连接池Hikari监控
    • Spring Boot中使用AOP统一处理Web请求日志
    • SpringBoot 三大开发工具,你都用过么?
    • Spring Boot 3.2 + CRaC = 王炸!
    • springboot启动的时候排除加载某些bean
    • spring boot中集成swagger
      • springboot项目引入这个包以后把原来的json报文改为了xml格式返回
      • SpringBoot中new对象不能自动注入对象和属性的问题
      • 使用 Spring Boot Actuator 监控应用
      • 记录一次springboot自动任务线上突然不执行问题排查过程
      • SpringBoot定时任务@Scheduled源码解析
      • Spring Boot + Lua = 王炸!
      • Spring Boot 实现定时任务动态管理
      • SpringBoot的@Async注解有什么坑?
      • druid 参数配置详解
      • Spring Boot HandlerMethodArgumentResolver 使用和场景
      • SpringBoot数据加解密
      • 解决controller层注入的service为null
      • 在 Spring Boot 中通过 RequestBodyAdvice 统一解码请求体
      • SpringBoot之使用Redisson实现分布式锁(含完整例子)
    • SpringCloudAlibaba

    • Nginx

    • 面试

    • 生产问题

    • 系统设计

    • 消息中间件

    • Java
    • SpringBoot
    程序员子龙
    2024-01-29
    目录

    spring boot中集成swagger

    swagger,中文“拽”的意思。它是一个功能强大的api框架,它的集成非常简单,不仅提供了在线文档的查阅,而且还提供了在线文档的测试。另外swagger很容易构建restful风格的api,简单优雅帅气,正如它的名字。

    Knife4j的前身是swagger-bootstrap-ui,前身swagger-bootstrap-ui是一个纯swagger-ui的ui皮肤项目。

    # 引入依赖

    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>knife4j-spring-boot-starter</artifactId>
        <!--在引用时请在maven中央仓库搜索2.X最新版本号-->
        <version>2.0.9</version>
    </dependency>
    
    1
    2
    3
    4
    5
    6

    # 创建Swagger配置依赖

    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 = "defaultApi2")
        public Docket defaultApi2() {
            Docket docket=new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(new ApiInfoBuilder()
                            //.title("swagger-bootstrap-ui-demo RESTful APIs")
                            .description("# swagger-bootstrap-ui-demo RESTful APIs")
                            .termsOfServiceUrl("http://www.xx.com/")
                            .contact("xx@qq.com")
                            .version("1.0")
                            .build())
                    //分组名称
                    .groupName("2.X版本")
                    .select()
                    //这里指定Controller扫描包路径
                    .apis(RequestHandlerSelectors.basePackage("com.demo.controller"))
                    .paths(PathSelectors.any())
                    .build();
            return docket;
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33

    # 添加接口

    @Api(tags = "首页模块")
    @RestController
    public class IndexController {
    
        @ApiImplicitParam(name = "name",value = "姓名",required = true)
        @ApiOperation(value = "测试")
        @GetMapping("/sayHi")
        public ResponseEntity<String> sayHi(@RequestParam(value = "name")String name){
            return ResponseEntity.ok("Hi:"+name);
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    启动Spring Boot工程,在浏览器中访问:http://localhost:8080/doc.html

    # 注解说明

    swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。

    • @Api:修饰整个类,描述Controller的作用
    • @ApiOperation:描述一个类的一个方法,或者说一个接口
    • @ApiParam:单个参数描述
    • @ApiModel:用对象来接收参数
    • @ApiProperty:用对象接收参数时,描述对象的一个字段
    • @ApiResponse:HTTP响应其中1个描述
    • @ApiResponses:HTTP响应整体描述
    • @ApiIgnore:使用该注解忽略这个API
    • @ApiError :发生错误返回的信息
    • @ApiParamImplicitL:一个请求参数
    • @ApiParamsImplicit 多个请求参数

    参考文档:https://doc.xiaominfo.com/knife4j/documentation/get_start.html

    上次更新: 2024/01/30, 15:08:57
    springboot启动的时候排除加载某些bean
    springboot项目引入这个包以后把原来的json报文改为了xml格式返回

    ← springboot启动的时候排除加载某些bean springboot项目引入这个包以后把原来的json报文改为了xml格式返回→

    最近更新
    01
    一个注解,优雅的实现接口幂等性
    11-17
    02
    MySQL事务(超详细!!!)
    10-14
    03
    阿里二面:Kafka中如何保证消息的顺序性?这周被问到两次了
    10-09
    更多文章>
    Theme by Vdoing | Copyright © 2024-2024

        辽ICP备2023001503号-2

    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式