[英]Generate Java methods from swagger yaml to be used with my own methods
[英]Generate java classes using swagger and yaml
我想使用 maven 插件swagger-codegen-maven-plugin
version 2.2.3
生成我的 Java 類。 這是我的 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
使用web服務。 所以我的方法是:
@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 文件以添加這些注釋。 有人可以幫助我嗎?
我找到了解決方案(不幸的是,這不是感謝 swagger 文檔)。 在我的 pom.xml 中插件的配置中,缺少<library>resteasy</library>
。 現在完整的配置是:
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.