• springboot 整合swagger


    springboot版本过高导致报错解决参考

    https://blog.csdn.net/nxg0916/article/details/123382012

    整合可以参考

    SpringBoot集成swagger-ui以及swagger分组显示_程序员青戈的博客-CSDN博客_springboot swagger 分组

    依赖

    1. <dependency>
    2. <groupId>io.springfoxgroupId>
    3. <artifactId>springfox-swagger2artifactId>
    4. <version>2.7.0version>
    5. dependency>
    6. <dependency>
    7. <groupId>io.springfoxgroupId>
    8. <artifactId>springfox-swagger-uiartifactId>
    9. <version>2.7.0version>
    10. dependency>

    配置类

    1. package com.angen.springboot_test2.config;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import springfox.documentation.builders.ApiInfoBuilder;
    5. import springfox.documentation.builders.PathSelectors;
    6. import springfox.documentation.builders.RequestHandlerSelectors;
    7. import springfox.documentation.service.ApiInfo;
    8. import springfox.documentation.service.Contact;
    9. import springfox.documentation.spi.DocumentationType;
    10. import springfox.documentation.spring.web.plugins.Docket;
    11. import springfox.documentation.swagger2.annotations.EnableSwagger2;
    12. @Configuration
    13. @EnableSwagger2
    14. public class SwaggerConfig {
    15. /**
    16. * 创建API应用
    17. * apiInfo() 增加API相关信息
    18. * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
    19. * 本例采用指定扫描的包路径来定义指定要建立API的目录。
    20. *
    21. * @return
    22. */
    23. @Bean
    24. public Docket restApi() {
    25. return new Docket(DocumentationType.SWAGGER_2)
    26. .groupName("标准接口")
    27. .apiInfo(apiInfo("Spring Boot中使用Swagger2构建RESTful APIs", "1.0"))
    28. .useDefaultResponseMessages(true)
    29. .forCodeGeneration(false)
    30. .select()
    31. //配置包扫描
    32. .apis(RequestHandlerSelectors.basePackage("com.angen.springboot_test2.controller"))
    33. .paths(PathSelectors.any())
    34. .build();
    35. }
    36. /**
    37. * 创建该API的基本信息(这些基本信息会展现在文档页面中)
    38. * 访问地址:http://ip:port/swagger-ui.html
    39. *
    40. * @return
    41. */
    42. private ApiInfo apiInfo(String title, String version) {
    43. return new ApiInfoBuilder()
    44. .title(title)
    45. .description("更多请关注: https://baidu.com")
    46. .termsOfServiceUrl("https://baidu.com")
    47. .contact(new Contact("xx", "https://baidu.com", "123456@163.com"))
    48. .version(version)
    49. .build();
    50. }
    51. }

    然后我们启动项目,打开http://ip:port/swagger-ui.html即可访问到接口文档


    RESTful风格接口

    1. /**
    2. * 通过id获取栏目
    3. */
    4. @GetMapping("getById/{id}")
    5. @ApiOperation(value = "根据id获取参数", notes = "根据id获取参数")
    6. @ApiImplicitParam(name = "id", value = "编号(必填)", required = true, dataType = "Integer", paramType = "path")
    7. public GameBigcate findById(@PathVariable("id") Integer id) {
    8. return cateService.findById(id);
    9. }

    常用注解

    @Api

    作用:作用在类上

    属性:

    1,tags:给类起一个别名,可以起多个

    @Api(tags = "栏目接口")

    @ApiOperation

    作用:可以做用在类上,或者作用在方法上。

    属性:

    1,value,给当前方法或者类一些描述 ,简单描述

    2,notes,一样的是可以给一些描述,详细描述

    @ApiOperation(value = "get请求,不用传递参数", notes = "栏目接口-通过get请求获取所有的顶级栏目")

    @ApiParam

    作用:主要作用在描述方法的参数

    属性:

    1,name:与传过来参数一致

    2,value:详细描述信息

    3,required:是否是必须的参数

    案例

    1. /**
    2. * 通过id获取栏目
    3. */
    4. @GetMapping("getById")
    5. @ApiOperation(value = "根据id获取参数", notes = "根据id获取参数")
    6. //@ApiImplicitParam(name = "id", value = "编号(必填)", required = true, dataType = "Integer", paramType = "path")
    7. public GameBigcate findById(
    8. @ApiParam(name="id",value ="顶级栏目id",required = true )
    9. @RequestParam Integer id) {
    10. return cateService.findById(id);
    11. }
    @ApiIgnore
    

    作用:忽略当前类或者方法或者参数不生成文档

    @ApiImplicitParam

    作用在方法上,在方法上描述方法的参数

    name:对应参数名
    value:描述
    required:是否必填传递
    dataType:数据类型
    paramType :查询参数类型,这里有几种形式:
            path 以地址的形式提交数据
            query 直接跟参数完成自动映射赋值
            body 以流的形式提交 仅支持POST
            header 参数在request headers 里边提交
            form 以form表单的形式提交 仅支持POST
    

    @ApiImplicitParams

    @ApiModel

    一般作用域实体类上,用于返回值类型

    属性:

    value:描述

    @ApiModelProperty

    作用在实体类属性上

    属性:

    value参数类型为String,作用为此属性的简要描述。

    name参数类型为String,作用为允许重写属性的名称。 字段名称

    allowableValues参数类型为String,作用为限制此参数存储的长度。

    access()参数类型为String,作用为允许从API文档中过滤属性

    notes参数类型为String,作用为该字段的注释说明

    dataType参数类型为String,作用为参数的数据类型。

    required参数类型为String,作用为指定参数是否可以为空,默认为false

    position参数类型为int,作用为允许显式地对模型中的属性排序。

    hidden参数类型为boolean,作用为是否允许模型属性隐藏在Swagger模型定义中,默认为false。

    example参数为String类型,作用为属性的示例值。

    readOnly参数类型为boolean,作用为是否允许将属性指定为只读,默认为false。

    reference参数类型为String,作用为指定对对应类型定义的引用,重写指定的任何其他数据名称。

    allowEmptyValue参数类型为boolean,作用为是否允许传递空值,默认为false

  • 相关阅读:
    Springboot+网上投资借贷中介服务 毕业设计-附源码221506
    Docker 日志管理 - ELK
    自动驾驶系统激光雷达传感器反射率标定板
    C语言第十一课(下):优化扫雷游戏
    PostgreSQL文本搜索(六)——词典
    地垫/毛绒地毯/地毯GCC认证测试项目介绍
    ECCV2022 商汤 发布最大的表征学习预训练数据集OmniBenchmark解读
    我要写整个中文互联网界最牛逼的JVM系列教程 | 「类加载子系统」章节:类的加载过程之二:Linking
    计算机网络【CN】子网划分与子网掩码
    【LeetCode】541. 反转字符串 II
  • 原文地址:https://blog.csdn.net/qq_42572322/article/details/126172154