java - 使用swagger和yaml生成java类

标签 java yaml swagger swagger-codegen

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

最佳答案

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

关于java - 使用swagger和yaml生成java类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46610466/

相关文章:

spring-boot - spring.jackson.default-property-inclusion=NON_NULL 在 spring boot 2.0 中添加到 application.yml 文件时不起作用

java - JPA persistence.xml 添加类

yaml - Hyperledger Fabric configtxgen - 读取配置时出错 : map merge requires map or sequence of maps as the value

java - 使用 Spring Boot 在 Google 应用引擎中执行 Cron 作业

initialization - Spring Boot - 从 application.yml 注入(inject) map<Enum,Class>

spring - 在 Spring Webflux 中使用 Swagger 生成 Web 服务描述

asp.net - 在 Angular 中使用 Swagger

java - Swagger UI 仅显示获取端点。

java - 如何扩展由 Liquibase 实现的数据库模式?

java - 出于测试目的终止 Java 线程