【问题标题】:Generate java classes using swagger and yaml使用 swagger 和 yaml 生成 java 类
【发布时间】:2018-03-18 12:56:24
【问题描述】:

我想使用 maven 插件 swagger-codegen-maven-plugin 版本 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 个类,然后我想使用 Web 服务创建 ChildAChildB。所以我的方法是:

@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 文件以添加这些注释。有人可以帮助我吗?

【问题讨论】:

    标签: java yaml swagger swagger-codegen


    【解决方案1】:

    我找到了解决方案(不幸的是,不感谢 swagger 文档)。在我的 pom.xml 中的插件配置中,&lt;library&gt;resteasy&lt;/library&gt; 丢失了。现在的完整配置是:

    <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>
    

    【讨论】:

    • 您是生成这些类以用于编写您的服务还是用于客户端?我需要生成类来编写我的 REST 服务,请在此处查看我的问题:*.com/q/48799907/1550811