4

I am trying to use Springdoc v2 with Spring Boot 3. It works fine when running it with the JVM, but not when running the native binary.

The Spring boot project is created like this: https://start.spring.io/#!type=maven-project&language=java&platformVersion=3.0.0&packaging=jar&jvmVersion=17&groupId=com.example&artifactId=demo&name=demo&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.demo&dependencies=native,web,lombok

I've added the Springdoc dependency:

<dependency>
   <groupId>org.springdoc</groupId>
   <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
   <version>2.0.0</version>
</dependency>

Starting the jar file, I can browse http://localhost:8080/swagger-ui/index.html, but when I run the native binary, I get a 404.

According to the documentation, it seems to support it out of the box: https://springdoc.org/v2/

Sim
  • 482
  • 4
  • 9

2 Answers2

2

You are using correct dependency

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.0.0</version>
</dependency>

but missing the property below

springdoc.enable-native-support=true
springdoc.swagger-ui.path=/swagger-ui

here the working example. After build you can see swagger ui working at http://localhost:8080/swagger-ui which will redirect to http://localhost:8080/swagger-ui/4.15.5/index.html

ozkanpakdil
  • 3,199
  • 31
  • 48
  • 1
    Based on your comment and the working example, I was able to get it working. See my detailed answer for details. I cannot undo my downvote, just yet, but will do so and add an upvote as soon as I can – Sim Dec 21 '22 at 06:29
  • After a lot of trial and error, I managed to get it working. Here's what I found out: Whitout setting any extra properties, the Swagger UI can be accessed by visiting http://localhost:8080/swagger-ui/4.15.5/index.html http://localhost:8080/swagger-ui gives a 404 http://localhost:8080/v3/api-docs works Setting springdoc.enable-native-support=true => Same result Setting springdoc.swagger-ui.path=/swagger-ui => Same result By setting both properties, I get a different behaviour. Visiting http://localhost:8080/swagger-ui/index.html still gives 404. – Sim Dec 21 '22 at 14:16
  • Visiting http://localhost:8080/swagger-ui I get redirected to http://localhost:8080/swagger-ui/4.15.5/index.html and am finally able to see the Swagger UI And where does 4.15.5 come from? This is the swagger-ui version currently used by springdoc-openapi: https://github.com/springdoc/springdoc-openapi/blob/master/pom.xml#L72 – Sim Dec 21 '22 at 14:16
  • if you check **mvn:dependency:tree** you will see **org.webjars:swagger-ui:jar:4.15.5:compile**, probably that is where 4.15.5 is coming from. – ozkanpakdil Dec 22 '22 at 06:37
  • yes, that is also what I found from the pom.xml file: https://github.com/springdoc/springdoc-openapi/blob/master/pom.xml#L72 – Sim Dec 22 '22 at 12:05
0

I test your project (swagger-boot3-graalvm) , it can be compile and genrate a single executable (by mvn clean package -Pnative native:compile), but when I ran the executable, the doc is 404. Then I tested it by:

java -Dspring.aot.enabled=true -jar ./target/demo-1.0-SNAPSHOT.jar

the document is OK. Then I tried to run:

java -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image -jar ./target/demo-1.0-SNAPSHOT.jar

and package (mvn clean package -Pnative native:compile) again. But compile error with Error: Detected a ZipFile object in the image heap.. My Compilation environment list:

Apache Maven 3.9.0 (9b58d2bad23a66be161c4664ef21ce219c2c8584)
Maven home: /root/apache-maven-3.9.0
Java version: 17.0.6, vendor: GraalVM Community, runtime: /root/graalvm-ce-java17-22.3.1
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.el7.x86_64", arch: "amd64", family: "unix"
Joman68
  • 2,248
  • 3
  • 34
  • 36
mark
  • 1