我有一个使用 Spring Boot 2.2.4.RELEASE 的 REST 应用程序。 我的 REST Controller 注释如下
@RestController
@RequestMapping("/")
我的 REST Controller 有 @GetMapping
、@PostMapping
等,它按预期工作。
现在我想在最新版本中集成Swagger。
https://github.com/swagger-api/swagger-core
https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Getting-started
其中显示的 Maven 依赖项是我添加到我的 pom.xml 中的:
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-core</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-models</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-integration</artifactId>
<version>2.1.1</version>
</dependency>
根据“入门”,只需添加依赖项,我就应该在 http://localhost:8080/openapi.json 处看到生成的 OpenAPI但那里什么也没有显示。
Swagger(swagger-core,...版本 2.1.1)可以与 Spring Boot Web 应用程序一起使用吗?
有一个 SpringFox 项目,但它不是最新的。 springdoc-openapi 也可用。但直接使用 Swagger 是我的第一个想法。
最佳答案
您的代码中需要一个 SwaggerConfig 文件。
Swagger for Java 所需的配置如下: (请注意,这只是基本文件,您可以根据需要配置它。)
@EnableSwagger2
@Configuration
public class SwaggerConfig {
public static String bucketName;
@Value("${swagger.config.basePackage}")
public void setName(String name) {
bucketName = name;
}
@Bean
public Docket classifiedApi()
{
ArrayList<ApiKey> apiKeys=new ArrayList<>();
apiKeys.add(apiKey());
return new Docket(DocumentationType.SWAGGER_2).securitySchemes(apiKeys)
.select()
.apis(RequestHandlerSelectors.basePackage(bucketName))
.build()
.apiInfo(metaData());
}
private ApiKey apiKey() {
return new ApiKey("Api Key", Constants.JWTToken.API_KEY, "header");
}
private ApiInfo metaData() {
return new ApiInfoBuilder()
.title("APPNAME REST API")
.description("Spring Boot REST API for APPNAME")
.contact(new Contact("YOURNAME ", "Coming Soon ...", "CONTACT ADDRESS"))
.build();
}
}
关于java - Spring Boot 2.2.4应用程序,使用Swagger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60373593/