我有一个简单的带注释的 API(一个 GET 和一个 POST 方法)。我正在尝试使用 swagger-maven-plugin 生成 Swagger API 规范文件。正在创建 Swagger API 规范文件,但问题是它只有 API 元数据,并且没有生成有关端点的信息,即没有生成“路径”字段。
我的 pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>rest-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rest-api</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.github.kongchen</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<apiSources>
<apiSource>
<springmvc>true</springmvc>
<locations>com.example.restapi.DriverController</locations>
<schemes>http</schemes>
<host>localhost:8080</host>
<basePath>/api</basePath>
<info>
<title>Swagger Maven Plugin Sample</title>
<version>v1</version>
<description>This is a sample for swagger-maven-plugin</description>
<contact>
<name>My name</name>
</contact>
<license>
<url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
<name>Apache 2.0</name>
</license>
</info>
<outputPath>${basedir}/generated/document.html</outputPath>
<outputFormats>json,yaml</outputFormats>
<swaggerDirectory>generated/swagger-ui</swaggerDirectory>
</apiSource>
</apiSources>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Driver.java 类:
package com.example.restapi;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(description = "This is the driver model")
public class Driver {
@ApiModelProperty(notes = "The id of the driver")
private int driverId;
@ApiModelProperty(notes = "The name of the driver")
private String driverName;
@ApiModelProperty(notes = "The name of the car")
private String carName;
public Driver(int driverId, String driverName, String carName) {
this.driverId = driverId;
this.driverName = driverName;
this.carName = carName;
}
}
DriverController.java 类:
package com.example.restapi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RequestMapping("/api")
@RestController
@Api(value = "Driver API Rest Controller")
public class DriverController {
private static List<Driver> drivers = new ArrayList<Driver>();
static{
drivers.add(new Driver(1,"Name1","VW"));
drivers.add(new Driver(2,"Name2","Honda"));
drivers.add(new Driver(3,"Name3","Audi"));
}
@GetMapping("/drivers")
@ApiOperation(value="Get list of all available drivers", response=List.class)
@ApiResponses(value={
@ApiResponse(code=200, message="Success, OK"),
@ApiResponse(code=404, message="Not found")
})
public List<Driver> getDrivers(){
return drivers;
}
@PostMapping("/driver")
@ApiOperation(value="Add a new driver to the list of all available drivers", response=Driver.class)
@ApiResponses(value={
@ApiResponse(code=200, message="Success, OK"),
@ApiResponse(code=404, message="Not found")
})
public List<Driver> createDriver(@RequestBody Driver driver){
drivers.add(driver);
return drivers;
}
}
SwaggerConfig.java 类:
package com.example.restapi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
//import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.restapi"))
.paths(PathSelectors.any())
.build();
}
}
swagger-maven-plugin 生成的 API 规范文件是(在运行mvn compile时):
{
"swagger":"2.8",
"info":{
"description" : "This is a sample for swagger-maven-plugin",
"version":"v1",
"title":"Swagger Maven Plugin Sample",
"contact":{
"name":"My name"
},
"license":{
"name":"Apache 2.0",
"url":"http://www.apache.org/license/LICENSE-2.0.html"
},
"host":"localhost:8080",
"basePath":"/api",
"schemes" :["http","https"]
}
}
如您所见,上述规范中没有关于端点的信息。请帮忙。
提前致谢!