Reputation: 67
I have a simple swagger annotated API (a GET and a POST method). I'm trying to generate the Swagger API specification file using the swagger-maven-plugin. The Swagger API specifications file is getting created but the problem is that it only has the API meta data in it and no info about the endpoints is getting generated i.e. the "paths" field is not getting generated.
My 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 class :
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 class :
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 class :
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();
}
}
The API specifications file generated by swagger-maven-plugin is (on running 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"]
}
}
As you can notice there is no information about the endpoints in the above specification. Please help.
Thanks in advance!
Upvotes: 0
Views: 820
Reputation: 93
I was facing the same issue, try using plugin version 3.1.7 instead of 3.1.1 and the also change the locations tag, try
<locations>
<location>com.example.restapi.DriverController</location>
</locations>
It worked for me after making these changes.
Upvotes: 0