Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Java Swagger Annotation to Swagger json schema? [closed]

How to generate JSON file from java files containing Swagger specific annotations, so that Swagger-UI can read it.

like image 711
Vivek Sinha Avatar asked Feb 22 '16 05:02

Vivek Sinha


People also ask

Does swagger use JSON schema?

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.

What is the use of swagger annotations?

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.


1 Answers

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.

like image 174
Simon Forsberg Avatar answered Sep 18 '22 15:09

Simon Forsberg