当前位置:   article > 正文

标题springboo3.x集成springdoc_springdoc-openapi-starter-webmvc-ui

springdoc-openapi-starter-webmvc-ui

pom文件

修改pom文件,引入相应jar文件.

		<dependency>
			<groupId>org.springdoc</groupId>
			<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
			<version>2.0.2</version>
		</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

openapi的配置文件

@Configuration
public class OpenapiConfiguration {

    @Bean
    public OpenAPI restfulOpenAPI() {
        Info info = new Info().title("Spring boot demo api project")
                .description("openapi-demo")
                .version("1.0.0")
                .license(new License().name("Apache 2.0").url("http://springdoc.org"));
        return new OpenAPI()
                .info(info)
                .externalDocs(new ExternalDocumentation()
                        .description("SpringDoc Wiki Documentation")
                        .url("https://springdoc.org/v3"));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

Web 控制器编写

@Tag(name = "演示组")
@RestController
@RequestMapping("/demo")
public class SwaggerDemoController {

    @Operation(method = "POST", summary = "RequestParam参数请求方式")
    @PostMapping(value = "/post-query", produces = {"application/json"})
    public ListResponse<City> getSchool(@Parameter(name = "schoolId", description = "学校ID", required = true, example = "88")
                                                          @RequestParam Integer schoolId,
                                                          @Parameter(name = "schoolName", description = "学校名称", required = false, example = "xx中心")
                                                          @RequestParam String schoolName) {
        return new ListResponse<City>(Collections.emptyList());
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

访问项目的swagger-ui的地址, 比如 http://localhost:9090/swagger-ui/index.html
在这里插入图片描述

兼容 Swagger 2.0

springdoc使用OpenAPI 3.0 规范来生成 API 文档的方式。虽然它不是直接兼容 Swagger 2.0,但是可以通过配置将 OpenAPI 3.0 规范转换为 Swagger 2.0 规范。
在项目的配置文件中增加如下配置即可.

springdoc:
  api-docs:
    enabled: true
    path: /v2/api-docs
  • 1
  • 2
  • 3
  • 4

springfox迁移到springdoc

springdoc中的产生文档注解使用时跟springfox不一致,对照关系如下

@Api → @Tag
@ApiIgnore → @Parameter(hidden = true) or @Operation(hidden = true) or @Hidden
@ApiImplicitParam → @Parameter
@ApiImplicitParams → @Parameters
@ApiModel → @Schema
@ApiModelProperty(hidden = true) → @Schema(accessMode = READ_ONLY)
@ApiModelProperty → @Schema
@ApiOperation(value = "foo", notes = "bar") → @Operation(summary = "foo", description = "bar")
@ApiParam → @Parameter
@ApiResponse(code = 404, message = "foo") → @ApiResponse(responseCode = "404", description = "foo")

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/107989
推荐阅读
相关标签
  

闽ICP备14008679号