使用 swagger 和 yaml 生成 java 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46610466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:17:51  来源:igfitidea点击:

Generate java classes using swagger and yaml

javayamlswaggerswagger-codegen

提问by cheb1k4

I would like to generate my Java classes using the maven plugin swagger-codegen-maven-pluginversion 2.2.3. Here my pom.xml file with the configuration:

我想使用 maven 插件swagger-codegen-maven-plugin版本生成我的 Java 类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>

My project.yaml file contains this:

我的 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"

All the 3 classes are generated and then I want to create ChildAor ChildBusing a web service. So my method is:

生成了所有 3 个类,然后我想创建ChildAChildB使用 Web 服务。所以我的方法是:

@POST
public Response createChild(@WebParam Parent parent) {
    ...
}

Using Postman, I sent the following json in order to create an ChildAinstance:

使用 Postman,我发送了以下 json 以创建一个ChildA实例:

{
    "code": "child-a",
    "attributeA": "value"
}

The following exception happens:

发生以下异常:

Caused by: com.fasterxml.Hymanson.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 Parentclass like:

我在几个地方读到我在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" ) })

But I don't know how to modify my yaml file to add those annotations. Can someone helps me?

但我不知道如何修改我的 yaml 文件以添加这些注释。有人可以帮助我吗?

回答by cheb1k4

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:

我找到了解决方案(不幸的是,这不是感谢 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>