Swagger-UI works generally fine when I run SpringBoot application directly in the IDE (by clicking run on main class) or via maven spring-boot:run plugin. Anyway it's broken when I run SpringBoot as JAR file trough java -jar
command in CLI.
How the app is built (modules):
root (no parent):
a. core (no parent)
b. rest-api (root as parent)
rest-api -> pom.xml fragment:
<dependencies>
<!-- MORE dependencies -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.14</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-security</artifactId>
<version>1.6.14</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.5</version>
<configuration>
<mainClass>example.package.MainBootApplication</mainClass>
<skip>false</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>example.package.MainBootApplication</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
main app class:
package example.package;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
@OpenAPIDefinition
@SpringBootApplication
public class MainBootApplication {
public static void main(String[] args) {
SpringApplication.run(BastKundApplication.class, args);
}
}
I tested following URLs:
a) http://localhost:8080/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config (works when it run with maven)
b) http://localhost:8080/swagger-ui/index.html (works when it run with maven)
c) http://localhost:8080/swagger-ui.html
I build my app with mvn clean package
command and it assembled a JAR with all dependencies (see rest-api/pom.xml content) and later java -jar /path/to/file.jar
. The application works fine and I can call my REST end-points properly but Swagger-UI is broken. When I run app trough main class with IDE Swagger works fine.
I can see a log info when the app starts directly trough IDE that SpringDoc initializes properly (after I call the URL) .
[2m2022-12-30 13:03:39.722[0;39m [32m INFO[0;39m [35m23776[0;39m [2m---[0;39m [2m[nio-8080-exec-8][0;39m [36mo.springdoc.api.AbstractOpenApiResource [0;39m [2m:[0;39m Init duration for springdoc-openapi is: 330 ms
This log doesn't appear when I run trough JAR file:
Any ideas?
I solved it by replacing maven-assembly-plugin
with spring-boot-maven-plugin
like below:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.5</version>
<configuration>
<mainClass>example.package.MainBootApplication</mainClass>
<skip>false</skip>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>jar-with-dependencies</classifier>
<mainClass>example.package.MainBootApplication</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
maybe it will help someone :-)