繁体   English   中英

Spring Boot 2.2.4 应用程序,使用 Swagger

[英]Spring Boot 2.2.4 application, using Swagger

我有一个使用 Spring Boot 2.2.4.RELEASE 的 REST 应用程序。 我的 REST 控制器被注释为

@RestController
@RequestMapping("/")

我的 REST 控制器有@GetMapping@PostMapping等,它按预期工作。

现在我想将 Swagger 集成到最新版本中。
https://github.com/swagger-api/swagger-core
https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---入门

那里显示的 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,... in version 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();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM