How to create POJO from Swagger document from Maven
- Dev Raj Sinha
- Jun 18, 2023
- 1 min read
To generate POJO classes from a Swagger document using Maven, you can utilize the Swagger Codegen plugin. The plugin integrates with Maven and allows you to generate Java classes based on the Swagger document during the build process. Here's how you can configure and use the Swagger Codegen plugin in your Maven project:
1. Add Swagger Codegen Plugin to pom.xml:
Open your project's `pom.xml` file and add the Swagger Codegen plugin configuration under the `<build>` section:
<build>
<plugins>
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.27</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>path/to/swagger.json</inputSpec>
<language>java</language>
<output>generated-sources</output>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
In this configuration, replace `path/to/swagger.json` with the actual path to your Swagger JSON or YAML document. The `<language>` element is set to `java` to generate Java classes. The generated classes will be placed in the `generated-sources` directory.
2. Run Maven Build:
Run the Maven build command to generate the POJO classes:
mvn clean install
During the build process, the Swagger Codegen plugin will read the Swagger document specified in the `<inputSpec>` element and generate the corresponding Java classes based on the specified `<language>` and output directory.
3. Find Generated POJO Classes:
After the Maven build completes successfully, you can find the generated POJO classes in the specified output directory (`generated-sources` in the example configuration). The classes will be available for use in your project.
By configuring and running the Swagger Codegen plugin in your Maven project, you can automate the process of generating POJO classes from a Swagger document during the build phase. This ensures that your API client or server-side code stays in sync with the API specification.