Spring boot 2.x集成swagger

1、官方文档

 

2、历史版本安装

 如果使用的不是最新的spring boot版本,则进入红框链接,打开历史版本列表。图片中也说明了springdoc-openapi v1.8.0 is the latest Open Source release supporting Spring Boot 2.x and 1.x.

 

3、2.x版本文档

https://springdoc.org/v1/

 

4、全局配置以及从SpringFox迁移的内容

https://springdoc.org/v1/#migrating-from-springfox

 

5、通过pom集成swagger

    <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-ui</artifactId>
      <version>1.8.0</version>
    </dependency>

注意spring boot对应的swagger的版本限制

 

6、修改swagger默认配置

 新增SwaggerConfiguration,注入到容器中.

 

7、测试代码

(1)、控制器代码如下:

@RequestMapping(value = "/test")
@RestController
@Tag(name="测试控制器",description = "测试控制器描述")
public class TestController
{
    @Operation(summary ="测试get方法",description = "测试get方法描述")
    @GetMapping(value = "/hello")
    public String get(@Parameter(required = true,name="字符串参数") String param,@Parameter(required = true,name="int参数")int intParam)
    {
        return param+intParam;
    }

    @Parameters({
            @Parameter(required = true,name="param",description = "字符串参数"),
            @Parameter(required = true,name="intParam",description = "int参数")
    })
    @Operation(summary ="测试get方法1",description = "测试get方法描述1")
    @GetMapping(value = "/hello1")
    public String get1(String param,int intParam)
    {
        return param+intParam;
    }

    @Operation(summary ="测试get方法2",description = "测试get方法描述2")
    @PostMapping(value = "/hello12")
    public TestRequestDto get2(@Parameter(required = true, name = "字符串参数") String param, @Parameter(required = true, name = "表单参数") @RequestBody TestRequestDto dto) {
        return dto;
    }
}

(2)、类型入参代码

@Data
@Schema(description= "测试入参Dto类型")
public class TestRequestDto
{
    @Schema(description= "字符串参数")
    private String param;

    @Schema(description="int参数")
    private int intParam;

    @Schema(description="decimal参数")
    private BigDecimal decimalParam;

    @Schema(description="bool参数")
    private boolean boolParam;

    @Schema(description="date参数")
    private Date dateParam;

    @Schema(description="float参数")
    private float floatParam;

    @Schema(description="double参数")
    private double doubleParam;

    @Schema(description="long参数")
    private Long longParam;

    @Schema(description="TestRequestInnerDto类型参数")
    private TestRequestInnerDto innerDto;

    @Schema(description="list参数")
    private List<String> listParam;
}

@Schema(description= "测试入参Dto内部类型")
@Data
class TestRequestInnerDto
{
    @Schema(description="字符串参数")
    private String param;

    @Schema(description="int参数")
    private int intParam;

    @Schema(description="decimal参数")
    private BigDecimal decimalParam;

    @Schema(description="bool参数")
    private boolean boolParam;

    @Schema(description="date参数")
    private Date dateParam;

    @Schema(description="float参数")
    private float floatParam;

    @Schema(description="double参数")
    private double doubleParam;

    @Schema(description="long参数")
    private Long longParam;
}

 

posted @ 2024-09-03 11:20  郑小超  阅读(33)  评论(0编辑  收藏  举报