2017-10-06 1301 views
0

我想用maven插件swagger-codegen-maven-plugin版本2.2.3生成我的Java类。在这里我的pom.xml的配置文件:使用swagger和yaml生成java类

<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类,然后我想用网络来创建ChildAChildB服务。所以,我的方法是:

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

使用邮差,我把下面的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: [email protected]; 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文件中添加的这些注释。有人可以帮助我吗?

回答

0

我找到了解决方案(不感谢招摇文档不幸)。在我的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> 
+1

您是否生成这些类用于编写您的服务或这些是为客户端?我有一个需要生成写我的REST服务,请参阅我的问题在这里类:https://stackoverflow.com/q/48799907/1550811 – Learner