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.
Related
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.
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>
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>
I am having a WSDL (lets say one.wsdl ) from which I want to generate JAXB artifacts using maven plugin. one.wsdl imports another wsdl (two.wsdl ) . When I am running the maven plugin to generate the JAXB artifacts , looks like its not recognizing two.wsdl and not generating the JAXB artifacts.
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.7.5</version>
<executions>
<execution>
<id>DataBindings_XJC_generate</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaDirectory>src/main/resources/wsdl/</schemaDirectory>
<schemaIncludes>
<include>one.wsdl</include>
</schemaIncludes>
<generateDirectory>src/main/java/</generateDirectory>
<generatePackage></generatePackage>
<readOnly>true</readOnly>
<removeOldOutput>true</removeOldOutput>
<verbose>true</verbose>
<extension>false</extension>
<forceRegenerate>true</forceRegenerate>
<episode>false</episode>
</configuration>
</execution>
</executions>
</plugin>
one.wsdl is refering to two.wsdl like this (both are in same directory).
<wsdl:import namespace="http://namespce:uri" location="two.wsdl"/>
However If i give the two.wsdl (in the include tag) in plugin , its able to generate the jaxb artifacts successfully.
Can anyone please suggest what might have gone wrong with the plugin when it comes to recognizing the imported WSDL ?
maven-jaxb2-plugin is just a wrapper for XJC. It does not do any schema processing on it own, it just calls XJC.
Please send me a test case as a PR request here:
https://github.com/highsource/maven-jaxb2-plugin/tree/master/tests
I'll check if this has something to do with the plugin (unlikely) or forward it to Oracle.
I think, this might be also by design - if your one.wsdl does not use anything from two.wsdl then nothing for the two.wsdl will be generated.
I have determined that two JAXB plugins for Maven 2 exist, with some different configurations.
The one is from Sun: http://jaxb.dev.java.net/jaxb-maven2-plugin/, the other from Mojohaus: http://mojohaus.org/jaxb2-maven-plugin/
Which of these two plugins can be recommended?
Thanks Matt. On my little research project, I found that there's quite another plugin comming from the sunners:
<groupId>com.sun.tools.xjc.maven2</groupId>
<artifactId>maven-jaxb-plugin</artifactId>
and that one:
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
and still the one from Codehouse.
Let's summarize. We have/had:
the maven-jaxb2-plugin (https://github.com/highsource/maven-jaxb2-plugin)
the maven-jaxb-plugin (https://jaxb.dev.java.net/jaxb-maven2-plugin/)
the jaxb2-maven-plugin (https://github.com/mojohaus/jaxb2-maven-plugin)
Based on the comments of this thread, I've always used the maven-jaxb2-plugin (i.e. plugin #1):
Concerning the
org.jvnet.jaxb2.maven2:maven-jaxb2-plugin
versus
com.sun.tools.xjc.maven2:maven-jaxb-plugin,
from my point of view it's definitely
the first one
(http://maven-jaxb2-plugin.java.net/).
This plugin has much more features
than
com.sun.tools.xjc.maven2:maven-jaxb-plugin,
the development is active. Finally,
I'm one of the authors :) and I'd say
we keep in touch with JAXB developers
and users and react to the latests
features/requests.
And indeed, the plugin #2 is dead. And because I've always been happy with #1, I've never used plugin #3 so can't really say anything about it. Just in case, here is a working configuration for plugin #1:
<project>
...
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I have recently tried the three plug-ins mentioned above (included here as well):
the maven-jaxb2-plugin (http://maven-jaxb2-plugin.java.net/)
the maven-jaxb-plugin (https://jaxb.dev.java.net/jaxb-maven2-plugin/)
the jaxb2-maven-plugin (http://mojo.codehaus.org/jaxb2-maven-plugin/)
I ended up using a fourth option: The CXF XJC Maven Plugin http://cxf.apache.org/cxf-xjc-plugin.html
If I am missing something I would like to know, but the configuration seemed more straightforward for what I was trying to do and more easily allowed me to to deal with duplicate class generation within the same namespace -- similar to this question: Is there a way to deal with duplicate element definitions across multiple .xsd files in JAXB?.
I now have granular control over each incoming XSD and corresponding java package; here is a sample configuration close to the one I am using.
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-xjc-plugin</artifactId>
<version>2.3.0</version>
<configuration>
<extensions>
<extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:2.3.0</extension>
</extensions>
</configuration>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>xsdtojava</goal>
</goals>
<configuration>
<sourceRoot>${basedir}/target/generated-sources/src/main/java</sourceRoot>
<xsdOptions>
<xsdOption>
<xsd>src/main/resources/schema/commands.xsd</xsd> <!--shares a common.xsd file causing the conflicts-->
<packagename>com.foo.bar.commands</packagename>
</xsdOption>
<xsdOption>
<xsd>src/main/resources/schema/responses.xsd</xsd>
<packagename>com.foo.bar.responses</packagename>
</xsdOption>
</xsdOptions>
</configuration>
</execution>
</executions>
</plugin>
I am the author of maven-jaxb2-plugin.
The maven-jaxb2-plugin currently uses JAXB 2.1. In the next versions we'll also provide JAXB 2.0 and JAXB 2.2 versions.
As for "which plugin is better" discussion, check the features, decide yourself. Let me know if you miss some functionality.
maven-jaxb2-plugin uses the JAXB reference implementation by Oracle/Sun
cxf and jaxb2-maven-plugin use Apache Xerces
On a slight tangent: there was a problem with use of maven-jaxb2-plugin with Eclipse Indigo that I posted here. A fix (extension) has recently become available.
This is not meant to disagree, at all, with the recommendation of maven-jaxb2-plugin over maven2-jaxb-plugin. I don't know, but I expect that maven2-jaxb-plugin has the same problem, probably unresolved.
I would guess that one is for the original JAXB specification and the codehaus one is for the JAXB 2.1 specification (and if the dev.java.net would load some time this century, I'd be able to say for sure).