Recently, we shared why we 💜 Swagger and how you can auto-generate it for your Django and Flask apps. Today, we’re going to share how you can auto-generate Swagger (or OpenAPI docs) for your Java Spring apps!
Here’s the deal: AppMap is a free and open source tool that you can use to automatically generate Swagger from running code. AppMap ensures your API documentation is always accurate and up-to-date – no tedious, time-consuming, manual labor required. YAY.
Here’s how AppMap Swagger generation works:
- An AppMap agent is added to the tool chain of your application as a new build dependency.
- When you run your tests, the AppMap agent records AppMap Data, which are visual, interactive maps of your application’s code. AppMap Diagrams include details about all of the web service requests made in your tests.
- The AppMap
openapitool generates OpenAPI documentation from the recorded AppMap Data.
You can execute this flow in either your local environment or in automated CI/CD pipelines. And you can download AppMap with OpenAPI generation for free for your Spring apps here.
Here’s a video walk-through of how to set up and use AppMap Swagger/OpenAPI generation for Java (we’ve included a script summary below in case you prefer to read vs. watch). Thanks for reading and watching!
-
00:00I am going to demonstrate how to automatically generate OpenAPI docs for my Java Spring application with AppMap. -
00:10My application is WebGoat - a deliberately insecure application that lets developers test vulnerabilities commonly found in Java-based applications that use common and popular open source components.You can find the WebGoat repository used in this demo here: https://github.com/land-of-apps/WebGoat.git
-
00:24I already have a Java environment set up and am ready to install AppMap and run tests. 00:30Installing AppMap is easy with the command line installation tool (which requires Node.js). In the WebGoat project folder I’ll run this:npx @appland/appmap installThe installer asks me to confirm the environment and sets up AppMap for the project automatically.
-
00:48WebGoat usesMavenand the AppMap installer adds the AppMap Maven plugin to the masterpom.xmlfile. A similar gradle plugin exists for gradle-built applications. -
01:01Let me quickly review thepom.xmlfiles of the project, because it’s always recommended to verify the build configuration of complex Java applications.The standard
surefireplugin configuration was modified and the change unfortunately breaks all Java agent plugins such as AppMap orjacoco. To quickly fix this issue, I’ve modified the surefire configuration:<configuration> <forkCount>1</forkCount> <reuseForks>true</reuseForks> <argLine> @{argLine} --illegal-access=permit </argLine> </configuration> 01:15Now I will run tests to record AppMap Data:./mvnw testIf I used Windows:
mvnw testWhen the tests finish, AppMap files will be stored in the
tmp/appmapsubfolders of all sub-modules that have tests.01:32In the final step, I will install and run the AppMapopenapicommand:npm install @appland/appmap npx @appland/appmap openapi --openapi-title "WebGoat" --openapi-version "8.2.0" -o webgoat-openapi.yaml --appmap-dir=.-
01:39And that’s it!webgoat-openapi.ymlis my generated OpenAPI documentation. If my application consisted of additional microservices, I would generate documentation from each service using the same approach: 1) install the AppMap agent, 2) run tests, 3) generate OpenAPI documentation from AppMap Data. -
02:05I can inspectwebgoat-openapi.ymlin my IDE or in any OpenAPI tool. Let me upload it toswagger.ionow. 02:11And here it is, the OpenAPI documentation of the WebGoat app generated and imported to swagger.io in minutes.