在项目中添加了 swagger-maven-plugin 以创建 OpenApi3 文档。以下是 pom.xml 中的插件设置:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.4.8</version>
</dependency>
...
<plugin>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>2.0.10</version>
<configuration>
<resourcePackages>
<package>analytics.api</package>
</resourcePackages>
<outputFileName>generated_swagger_apiDoc</outputFileName>
<outputPath>${project.basedir}/generated-swagger</outputPath>
<outputFormat>JSONANDYAML</outputFormat>
<prettyPrint>true</prettyPrint>
</configuration>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>resolve</goal>
</goals>
</execution>
</executions>
</plugin>
我已经注释了java类:
{more imports...}
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
{more imports...}
@Tag(
name = "Needed name",
description = "API description"
)
@RestController
@RequestMapping("/request")
@RequiredArgsConstructor
public class Example {
private final Service service;
private final ObjectMapper objectMapper;
@Operation(
summary = "Operation description",
method = "post"
)
@ApiResponse(responseCode = "200", description = "Request successful")
@ApiResponse(responseCode = "400", description = "Bad request")
@ApiResponse(responseCode = "500", description = "Internal server error")
@PostMapping("/request")
public void method(
@Parameter(
description = "Post request in JSON",
required = true,
example = "{json request example}"
)
@RequestParam("request") String request, HttpServletResponse res) throws IOException {
//any code...
}
}
mvn install 之后我得到了一个文件 generated_swagger_apiDoc.json,但这就是内容:
{
"openapi" : "3.0.1"
}
我尝试了不同版本的 springdoc-openapi-ui,在 api-doc url 上有一个很好的大 json 文件,其中包含我的所有类。我需要将 OpenAPI3 规范文档导出到 json 文件,我该怎么做?
我找到了替代方案,但忘了在这里提及。无论如何,这就是我发现的。
<dependency> <groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.3.9</version> </dependency>
我创建了一个附加配置文件并将其添加到其中:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.8.RELEASE</version>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<apiDocsUrl>swaggerAddressHere<apiDocsUrl>
<outputFileName>document-${project.artifactId}.json</outputFileName>
<outputDir>${project.basedir}/generated-swagger</outputDir>
</configuration>
</plugin>
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.46</version>
<executions>
<execution>
<phase>post-integration-test</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
${project.basedir}/generated-swagger/document-${project.artifactId}.json
</inputSpec>
<language>html</language>
<output>${project.basedir}/generated-swagger</output>
</configuration>
</execution>
</executions>
</plugin>
我认为问题是这样的:
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>