Generating Java classes for Request and Response objects from Yaml file - java

We have created a Yaml file using Swagger editor for our APIs specification which includes Base URL, endpoint, Request, Response and Header information etc.. Now I want to implement RESTful web service for these APIs. For that I am thinking of generating my Request and Response Java classes from this Yaml file and was looking for some kind of code generator, preferably a maven plugin/dependency which I could use in my Maven project. I came across this rest client with swagger which talks about using the swagger-codegen Maven plugin, but this is to generate the client which I believe is about generating the client code to consume these RESTful APIs, however my need is to generate classes to be used for service implementation. I will be using Java and Spring framework.
My question is what are the best practices for implementing the RESTful web services in Java when we have Yaml file (API spec created using Swagger editor) and which code generation tools/plugins are available to be used.
EDIT: Just came across this Server stub generator HOWTO, looking further into it.

Swagger-codegen maven plugin is a good option but I suggest you to use jhipster to generate your java project. It generates projects with latest tech stack including spring framework. You can select API-First development in your case. I used it and it was very efficient. You already have Yaml file. Put it in src/main/resources/swagger/api.yml and run
./mvnw generate-sources
All java codes will be generated.

Using swagger-codegen-maven plugin like the following
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.4.29</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${basedir}/src/main/resources/swagger/project.yaml</inputSpec>
<language>java</language>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
<library>resteasy</library>
</configOptions>
</configuration>
</execution>
</executions>
or using openapi-generator-maven-plugin
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.3.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
${project.basedir}/src/main/resources/swagger/project.yaml
</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>where api package is to be rendered</apiPackage>
<modelPackage> model package </modelPackage>
<supportingFilesToGenerate>
Any supporting files needed </supportingFilesToGenerate>
<configOptions>
<delegatePattern>true</delegatePattern>
</configOptions>
</configuration>
</execution>
</executions>

Related

Mustache + Spring boot + REST API

I decided to make a project using Mustache + Spring Boot + REST API. Earlier, I used to write projects with the front-end used Java EE + Servlets + JSP. Now I decided to try to switch to Spring and write an application with a front-end. And I found a way to create a page on the front-end using Mustache, but if you use Mustache, then everything needs to be saved to the model, and I want to use REST API. I mean if it is possible? If it's possible I'll be happy if you show a piece of code your RestController and Mustache.
The question is 'if possible call endpoint from mustache?'.
Yes, You can create REST API using OpenAPI(https://support.smartbear.com/swaggerhub/docs/tutorials/openapi-3-tutorial.html) which uses the mustache file to create your code.
You need to add following dependencies,
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>0.2.2</version>
</dependency>
add the following plugin in the pom.xml for configuration(https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-maven-plugin/README.md),
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.0.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<templateDirectory>${project.basedir}/templates
</templateDirectory>
<configOptions>
//add the configuration options here
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
By default it uses the mustache files internally to generate codes, you can change the mustache files from the git location(https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/JavaSpring/)/create your own with the name mentioned in git and place it in the location ${project.basedir}/templates it will generate your codes during the compile time and you can use them in your project.
Place the OpenAPI document in the resource folder where application.properties is placed

Open API Maven Plugin - Generate only specifc API model classes

I am working on generation of model classes by Open API Maven plugin which I need to use in my business logic. The api is generated by other team and given to us. It has lot of model classes specification but I need only generation of 3 model classes from the api.yaml template
How this can be achieved?
The plugin configuration
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-java-client</id>
<configuration>
<inputSpec>
${project.build.directory}/../../../../../../../../../component/service/app/src/main/resources/api/v1/api-v1.yaml
</inputSpec>
**<modelPackage>com.example.editing.model</modelPackage>**
<generateSupportingFiles>false</generateSupportingFiles>
<generateApis>false</generateApis>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
</configuration>
</execution>
</executions>
</plugin>
According to the github readme of the openapi-generator-maven-plugin, there is a configuration option called modelsToGenerate which does exactly what you want:
A comma separated list of models to generate. All models is the default.

How to generate java entity by Graphql schema

I'm a newcomer for graphql java client development. Recently I meet a problem that how to generate a java entity through the graphql schema definition. Since in my side, the schema is always changing by another side, so I want a method to generate the java entity class automatically instead of reading the schema manually then change the code. If there are any practices on such a requirement? Thanks.
You could look into the following maven plugin. It would help you generate Java classes from your .graphqls files.
Assuming you have only one file defining your GraphQL model, named myGraphqls, I would use this plugin as follow:
<build>
<plugins>
<plugin>
<groupId>io.github.kobylynskyi</groupId>
<artifactId>graphql-codegen-maven-plugin</artifactId>
<version>1.2.3</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<graphqlSchemaPaths>
<graphqlSchemaPath>${project.basedir}/src/main/resources/graphql/myGraphqls.graphqls</graphqlSchemaPath>
</graphqlSchemaPaths>
<outputDir>${project.build.directory}/generated-sources/graphql</outputDir>
<packageName>com.graphql.model</packageName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
After running maven, you would be able to find in the generated-sources folder, a folder named graphql with the classes generated under the package com.graphql.model.

SOAP service in spring boot

I'm confused about the way I'm compelled to create a SOAP service in spring boot. I wrote many SOAP services in Java in the past, simply writing down the java code and not a line of XML. A very easy and error less approach.
All the tutorial I read for Spring Boot has the need to write an XSD document, form which maven will read and build the needed classes.
Is there a way, supported by Spring Boot, to bypass the XSD file and write directly the needed java classed instead?
Just to give you some example of what I read, here are some links to the tutorials I'm referring to:
https://www.concretepage.com/spring-boot/spring-boot-soap-web-service-example
https://howtodoinjava.com/spring-boot/spring-boot-soap-webservice-example/
https://www.javainuse.com/spring/springbootsoapwebservice
I believe that writing the XML code to generate the Java one is very error prone and a really difficult to maintain code, when you're developing big services or services with a lot of objects. All works fine if you have a small project, that is not my case.
What I did was to write the classes and then generate the xsd file using schemagen.
This is the configuration in maven pom I added:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>schemagen</id>
<goals>
<goal>schemagen</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${basedir}/src/main/resources/xsds/</outputDirectory>
<transformSchemas>
<transformSchema>
<uri>http://test/test-ws/MyTestSchema</uri>
<toPrefix>test</toPrefix>
<toFile>test.xsd</toFile>
</transformSchema>
</transformSchemas>
<sources>
<source>${basedir}/src/main/java/my/classes/</source>
</sources>
<verbose>true</verbose>
</configuration>
</plugin>

Unable to use generate models with jodatime using Swagger-codegen maven plugin for jersey2

Using the following configuration for the maven swagger-codegen plugin:
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.2</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${basedir}/src/main/resources/swagger.yaml</inputSpec>
<ignoreFileOverride>${basedir}/.swagger-codegen-ignore</ignoreFileOverride>
<language>jaxrs</language>
<library>jersey2</library>
<configOptions>
<sourceFolder>src/main/java</sourceFolder>
<apiPackage>com.github.jmccloskey.api</apiPackage>
<modelPackage>com.github.jmccloskey.model</modelPackage>
<invokerPackage>com.github.jmccloskey.invoker</invokerPackage>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
The code generation works, but uses Java Date in the models that are generated. From the swagger-codegen documentation this can be overridden to use JodaTime by adding the following inside the configOptions:
<dateLibrary>joda</dateLibrary>
This is what I am trying to do, but I get a compilation failure:
.../JodaDateTimeProvider.java:[3,41] package com.sun.jersey.core.spi.component does not exist
I think spi is what Jersey1 uses for dependency injection, whereas Jersey2 uses hk2.
I was able to resolve the compilation failure by adding jersey-bundle to my dependencies (along with a bunch of others such as jettison, rome and javax.mail) but had runtime issues in my API:
2. java.lang.IllegalStateException: Unable to perform operation: method inject on com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App
3. java.lang.IllegalStateException: Unable to perform operation: create on org.glassfish.jersey.message.internal.MessageBodyFactory
Mixing in Jersey 1 dependencies is not the right approach.
Is there a way to configure the swagger-codegen maven plugin to use hk2 instead?
For a full sample project see this github repo. Any pointers in the right direction would be appreciated.

Categories

Resources