How to generate JSON file from java files containing Swagger specific annotations, so that Swagger-UI can read it.
Lucas, Swagger uses an extended subset of JSON Schema do describe models. If you describe something outside that scope using JSON Schema, it will not be supported by Swagger.
X Annotations uses reflection to generate OpenApi definitions out of code. Annotated classes, value objects, methods, and parameters are used as input to the swagger generator. The output is a swagger definition file, it can be used then to generate client/server code and API docs.
As you say "I don't want to do it manually", use the Swagger Maven plugin:
<plugin>
<groupId>com.github.kongchen</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>3.1.4</version>
<configuration>
<apiSources>
<apiSource>
<springmvc>true / false</springmvc>
<locations>
<location>com.yourpackage.something</location>
</locations>
<host>yourhost.com</host>
<basePath>/some/path</basePath>
<info>
<title>Your Project Title</title>
<version>${project.version}</version>
<description>Some nice stuff</description>
<termsOfService>...</termsOfService>
<contact>
<email>[email protected]</email>
<name>Your Name</name>
<url>www.where.to.find.you</url>
</contact>
</info>
<swaggerDirectory>path/to/swagger/output</swaggerDirectory>
<outputFormats>json</outputFormats>
</apiSource>
</apiSources>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
Then you can do mvn compile
and it will generate the swagger.json
file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With