嗨,升级到 Spring Boot 3 后,我遇到了 swagger ui 的问题。swagger-ui 不再工作,我得到了 404 和“白标签”页面作为响应。
仔细一看我需要改变 来自:实现(组:'org.springdoc',名称:'springdoc-openapi-ui',版本:'1.5.8')
到:实现(组:'org.springdoc',名称:'springdoc-openapi-starter-webmvc-ui',版本:'2.0.3')
之后它确实起作用了,但是在我的另一个项目中,我们遇到了依赖冲突的问题,因此 swagger-ui 在尝试获取 /v3/api-docs/ 时抛出了 500 错误: 之间: io.confluence:kafka-avro-serializer:7.3.1 和 springdoc-openapi-starter-webmvc-ui:2.0.3 这2个依赖需要使用io.swagger.core.v3:swagger-annotations-jakarta,但是版本不同
所以在 gradle 中我必须解决冲突以强制 io.swagger.core.v3:swagger-annotations-jakarta:2.2.8.
如果您从 spring 2 升级到 3,您必须使用:
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.3'// swagger ui / openapi 3.0
而不是:
implementation 'org.springdoc:springdoc-openapi-ui:1.5.8'
如果 swagger-ui 给你一个 /v3/api-docs/ 的错误,可能是因为依赖冲突,所以我能够通过强制依赖来解决它:
错误:
Caused by: java.lang.NoSuchMethodError: 'io.swagger.v3.oas.annotations.media.Schema$AdditionalPropertiesValue io.swagger.v3.oas.annotations.media.Schema.additionalProperties()'
at io.swagger.v3.core.util.AnnotationsUtils.getSchemaFromAnnotation(AnnotationsUtils.java:552) ~[swagger-core-jakarta-2.2.8.jar:2.2.8]
at io.swagger.v3.core.util.AnnotationsUtils.getSchema(AnnotationsUtils.java:1170) ~[swagger-core-jakarta-2.2.8.jar:2.2.8]
at org.springdoc.core.service.GenericParameterService.setSchema(GenericParameterService.java:323) ~[springdoc-openapi-starter-common-2.0.3.jar:2.0.3]
at org.springdoc.core.service.GenericParameterService.buildParameterFromDoc(GenericParameterService.java:299) ~[springdoc-openapi-starter-common-2.0.3.jar:2.0.3]
at org.springdoc.core.service.AbstractRequestService.build(AbstractRequestService.java:321) ~[springdoc-openapi-starter-common-2.0.3.jar:2.0.3]
at org.springdoc.api.AbstractOpenApiResource.calculatePath(AbstractOpenApiResource.java:472) ~[springdoc-openapi-starter-common-2.0.3.jar:2.0.3]
at org.springdoc.api.AbstractOpenApiResource.calculatePath(AbstractOpenApiResource.java:652) ~[springdoc-openapi-starter-common-2.0.3.jar:2.0.3]
at org.springdoc.webmvc.api.OpenApiResource.lambda$calculatePath$11(OpenApiResource.java:219) ~[springdoc-openapi-starter-webmvc-api-2.0.3.jar:2.0.3]
使用 gradle 解决依赖关系:
configurations.all {
resolutionStrategy {
eachDependency { details ->
if (details.requested.group == 'io.swagger.core.v3') {
details.useVersion("2.2.8")
details.because('Swagger ui and Kafka Avro serializer incoptable dependency io.swagger.core.v3:swagger-annotations')
}
对于上述错误,只需使用命令确保所有依赖项都兼容
mvn dependency:tree
就我而言,io.swagger.core.v3:swagger-models
和io.swagger.core.v3:swagger-annotations
依赖项版本与 springdoc-openapi-starter-webmvc-ui 依赖项不兼容,因此更改版本后错误得到解决
另一种解决方案可能是从 springdoc-openapi 中排除 swagger-annotations 依赖项并手动包含较新的版本
//swagger
// exclude swagger annotation and use newer version due conflicts with avro serializer
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0"){
exclude group: "io.swagger.core.v3", module:"swagger-annotations"
}
// https://mvnrepository.com/artifact/io.swagger.core.v3/swagger-annotations
implementation 'io.swagger.core.v3:swagger-annotations:2.2.15'