Generate java classes using swagger and yaml
I found the solution (not thanks to swagger documentation unfortunately). In the configuration of the plugin in my pom.xml, <library>resteasy</library>
was missing. The full configuration is now:
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.3</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>
</plugin>
cheb1k4
Updated on May 08, 2020Comments
-
cheb1k4 almost 4 years
I would like to generate my Java classes using the maven plugin
swagger-codegen-maven-plugin
version2.2.3
. Here my pom.xml file with the configuration:<plugin> <groupId>io.swagger</groupId> <artifactId>swagger-codegen-maven-plugin</artifactId> <version>2.2.3</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> </configOptions> </configuration> </execution> </executions> </plugin>
My project.yaml file contains this:
definitions: Parent: type: "object" discriminator: "type" required: - type properties: id: type: "integer" format: "int64" code: type: "string" ChildA: allOf: - $ref: "#/definitions/Parent" - properties: attributeA: type: "string" ChildB: allOf: - $ref: "#/definitions/Parent" - properties: attributeB: type: "string"
All the 3 classes are generated and then I want to create
ChildA
orChildB
using a web service. So my method is:@POST public Response createChild(@WebParam Parent parent) { ... }
Using Postman, I sent the following json in order to create an
ChildA
instance:{ "code": "child-a", "attributeA": "value" }
The following exception happens:
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "attributeA" (class io.swagger.client.model.Parent), not marked as ignorable (2 known properties: "code", "id"]) at [Source: io.undertow.servlet.spec.ServletInputStreamImpl@1df2f416; line: 3, column: 17] (through reference chain: io.swagger.client.model.Parent["attributeA"])
I read at several places that I need some annotation in my
Parent
class like:@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") @JsonSubTypes({ @Type(value = ChildA.class, name = "ChildA"), @Type(value = ChildB.class, name = "ChildB" ) })
But I don't know how to modify my yaml file to add those annotations. Can someone helps me?
-
Learner about 6 yearsare you generating these classes to be used in writing your Service or these are for client side? I have a need to generate classes for writing my REST service, please see my questions here: stackoverflow.com/q/48799907/1550811