我正在尝试将 Spring Boot 连接到 Swagger UI。我正在使用这个依赖项:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
当我尝试通过 /swagger-ui/index.html 访问 Swagger UI 时,出现 500 错误:
application.yaml
中的当前配置我有:
springdoc:
api-docs:
path: "/api-docs"
配置:
import org.springframework.context.annotation.Configuration;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
@OpenAPIDefinition
@Configuration
public class SpringdocConfig {
@Bean
public OpenAPI baseOpenAPI() {
return new OpenAPI().info(new Info().title("Dashboard API").version("1.0.0").description("Dashboard API"));
}
}
错误:
2023-07-04T11:15:18.312+02:00 DEBUG 33532 --- [-nio-443-exec-8] o.s.web.servlet.DispatcherServlet : Failed to complete request: jakarta.servlet.ServletException: Handler dispatch failed: java.lang.NoSuchMethodError: 'io.swagger.v3.oas.annotations.media.Schema$RequiredMode io.swagger.v3.oas.annotations.media.Schema.requiredMode()'
2023-07-04T11:15:18.313+02:00 ERROR 33532 --- [-nio-443-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed: java.lang.NoSuchMethodError: 'io.swagger.v3.oas.annotations.media.Schema$RequiredMode io.swagger.v3.oas.annotations.media.Schema.requiredMode()'] with root cause
java.lang.NoSuchMethodError: 'io.swagger.v3.oas.annotations.media.Schema$RequiredMode io.swagger.v3.oas.annotations.media.Schema.requiredMode()'
at org.springdoc.core.configuration.SpringDocRequiredModule$RespectSchemaRequiredAnnotationIntrospector.hasRequiredMarker(SpringDocRequiredModule.java:52) ~[springdoc-openapi-starter-common-2.1.0.jar:2.1.0]
at com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair.hasRequiredMarker(AnnotationIntrospectorPair.java:319) ~[jackson-databind-2.14.1.jar:2.14.1]
at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getMetadata(POJOPropertyBuilder.java:230) ~[jackson-databind-2.14.1.jar:2.14.1]
at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector._anyIndexed(POJOPropertiesCollector.java:1240) ~[jackson-databind-2.14.1.jar:2.14.1]
我希望在 Swagger UI 中看到控制器的端点。
更新到 spring 3 后我遇到了同样的问题。
我将此依赖项添加到 openapi-starter 中:
implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0"
implementation "io.swagger.core.v3:swagger-annotations:2.2.15"
希望这对您有用。
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-starter-webmvc-ui -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.swagger.core.v3/swagger-core -->
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-core</artifactId>
<version>2.2.22</version>
</dependency>
@OpenAPIDefinition(
info = @Info(
title = "Any title you like",
version = "1.0.0",
description = "Any description",
termsOfService = "Terms of service",
// Others
),
security = {
@SecurityRequirement(name = "bearerAuth")
}
)
@SecurityScheme(
name = "bearerAuth",
description = "JWT authentication",
scheme = "bearer",
type = SecuritySchemeType.HTTP,
bearerFormat = "JWT",
in = SecuritySchemeIn.HEADER
)
public class SwaggerApiConfig {
}