Я хотел бы сгенерировать свои классы Java с помощью плагина maven swagger-codegen-maven-plugin
версии 2.2.3
. Вот мой файл pom.xml с конфигурацией:
<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>
Мой файл project.yaml содержит это:
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"
Все 3 класса сгенерированы, а затем я хочу создать ChildA
или ChildB
с помощью веб-службы. Итак, мой метод:
@POST
public Response createChild(@WebParam Parent parent) {
...
}
Используя Postman, я отправил следующий файл json, чтобы создать экземпляр ChildA
:
{
"code": "child-a",
"attributeA": "value"
}
Происходит следующее исключение:
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"])
Я читал в нескольких местах, что мне нужны аннотации в моем классе Parent
, например:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({ @Type(value = ChildA.class, name = "ChildA"),
@Type(value = ChildB.class, name = "ChildB" ) })
Но я не знаю, как изменить свой файл yaml, чтобы добавить эти аннотации. Может кто-нибудь мне поможет?