端点规范("paths"字段)未在由 swagger-maven-plugin 生成的 Swagger API 规范文件中生成



我有一个简单的swagger注释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"]
}
}

正如您所注意到的,在上述规范中没有关于端点的信息。请帮忙。

提前感谢!

我也面临同样的问题,请尝试使用插件版本3.1.7而不是3.1.1以及更改位置标签,尝试

<locations>
<location>com.example.restapi.DriverController</location>
</locations>

在做出这些改变后,它对我起了作用。