REST API Codegenerator Maven Plugin
Are you sick of always having to implement and maintain the client-side services for reaching your Rest API. With the REST API Codegenerator Maven Plugin you are able to automatically generate the client-side code to reach your API.
For now the plugin scans your code for Jersey resources and generates java code - see example of usage in the akka-persistence-java-example-project. In my next project I will extend the plugin to generate ReactJS code aswell.
If you find the plugin useful please give it a star. Also feel free to create pull requests and extend the plugin to generate for example Ajax, AngularJS or ReactJS code.
How to use the plugin for generating Java code
- Add the plugin to your
pom.xml
file, see example below. - Run
mvn clean install
in your project folder - The plugin will scan your source code and generate code out of your resource classes.
Plugin example for generating Java and React code
-
generatedJavaCodeFolder
: This is the folder where you want your generated Java code -
generatedJavaCodePackage
: This is the package of your generated class files -
generatedReactCodeFolder
: This is the folder where you want your generated React code -
rootHost
: This is the host of your running application
<plugin>
<groupId>no.jansoren</groupId>
<artifactId>restapi-codegen-maven-plugin</artifactId>
<version>1.0.3-SNAPSHOT</version>
<configuration>
<generatedJavaCodeFolder>../server-qtest/src/main/java</generatedJavaCodeFolder>
<generatedJavaCodePackage>com.example.qtest.services</generatedJavaCodePackage>
<generatedReactCodeFolder>../client/src/services</generatedReactCodeFolder>
<rootHost>http://localhost:8080</rootHost>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>codegen</goal>
</goals>
</execution>
</executions>
</plugin>
Dependencies required
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.25</version>
</dependency>
If you are using MediaType.APPLICATION_JSON_TYPE
you also need this dependency
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.25</version>
</dependency>
How to get started developing
- Clone this repo
git clone git@github.com:jansoren/restapi-codegen-maven-plugin.git
- Run
mvn clean install
in therestapi-codegen-maven-plugin
-folder - The unit tests have now generated some example code at /src/test/java/com/example/helloworld/MyService.java
Why this plugin
Over the years I have created a few REST API’s, and I always end up using a lot of time implementing the client-side code for reaching the API’s. In one of the projects we had a Quality-test application implemented in java that ran tests continuously, something that required updating the client-side code rapidly. With this challenge we ended up creating a lot of the same boilerplate code for each new service we implemented, and it felt like this code should and could be automatically generated.
In the pursuit of finding such a product I ended up creating a plugin to solve my current need. The result became this plugin.
I hope this can be useful for others as well.