Both diegomtassis & Erdinc Ay together with this great baeldung post got me on the right track.
The thing about the springdoc-openapi-maven-plugin is: it isn't able to successfully run on it's own - nor does it suffice to simply add it as the only dependency to your pom.xml
!
The plugin needs 2 other dependencies to do the groundwork in order to make it work:
- You need to add the springdoc-openapi-ui plugin (for Tomcat / Spring MVC based apps) OR the springdoc-openapi-webflux-ui plugin (for Reactive WebFlux / Netty based apps) to your
pom.xml
first!
Here's the Spring MVC example:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.4.8</version>
</dependency>
- You need to configure the
spring-boot-maven-plugin
to fire up our Spring Boot app including the API url the springdoc-openapi-maven-plugin
needs to generate the openapi.json
inside the Maven integration test phase.
Here's the needed configuration of the already present spring-boot-maven-plugin
inside your pom.xml
(and no: you don't need a version for this plugin here, since it's inheritet from the spring-boot-starter-parent
you use):
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
As the docs state you can customize things like apiDocsUrl
, outputDir
where the openapi.json
will be placed - and even the name of the json file with outputFileName
. But you don't need to do so (in contrast to what Peeve said)!
Now if you run Maven using mvn verify
(or mvn verify -DskipTests=true
to speed up the execution), you should see some output like that:
[INFO] --- spring-boot-maven-plugin:2.3.5.RELEASE:start (pre-integration-test) @ hellobackend ---
[INFO] Attaching agents: []
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.5.RELEASE)
2020-11-04 10:26:07.246 INFO 42143 --- [ main] i.j.s.SpringBootBuildpackApplication : Starting SpringBootBuildpackApplication on PikeBook.fritz.box with PID 42143 (/Users/jonashecht/dev/spring-boot/spring-boot-kong/hellobackend/target/classes started by jonashecht in /Users/jonashecht/dev/spring-boot/spring-boot-kong/hellobackend)
2020-11-04 10:26:07.249 INFO 42143 --- [ main] i.j.s.SpringBootBuildpackApplication : No active profile set, falling back to default profiles: default
2020-11-04 10:26:08.730 INFO 42143 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080
2020-11-04 10:26:08.742 INFO 42143 --- [ main] i.j.s.SpringBootBuildpackApplication : Started SpringBootBuildpackApplication in 1.82 seconds (JVM running for 2.318)
[INFO]
[INFO] --- springdoc-openapi-maven-plugin:1.1:generate (default) @ hellobackend ---
2020-11-04 10:26:09.579 INFO 42143 --- [ctor-http-nio-2] o.springdoc.api.AbstractOpenApiResource : Init duration for springdoc-openapi is: 29 ms
[INFO]
[INFO] --- spring-boot-maven-plugin:2.3.5.RELEASE:stop (post-integration-test) @ hellobackend ---
[INFO] Stopping application...
2020-11-04 10:26:09.661 INFO 42143 --- [on(2)-127.0.0.1] inMXBeanRegistrar$SpringApplicationAdmin : Application shutdown requested.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 23.392 s
[INFO] Finished at: 2020-11-04T10:26:11+01:00
[INFO] ------------------------------------------------------------------------
Finally you should have a new file openapi.json
inside your Spring Boot app's /target
folder like this (which was the point of using the springdoc-openapi-maven-plugin in the first place).
If you like to see a fully comprehensible example project, you can have a look at this one: https://github.com/jonashackt/spring-boot-openapi-kong/tree/main/weatherbackend
application.json
is supposed to reside. My guess is that they took the stance of "someone else generated theapplication.json
, we do not have to re-create it. We just start the server and fetch it". – Visitorspring-boot-maven-plugin
– Visitor