强制杰克逊以MISMO标准返回json对象

时间:2018-07-25 21:05:30

标签: java json spring-boot yaml naming-conventions

在我的maven pom文件中,我正在使用swagger-codegen-maven-pluginyaml文件生成模型:

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>src/main/resources/profile.yaml</inputSpec>
                <language>java</language>
                <output>${project.basedir}</output>
                <apiPackage>com.bki.api.profile</apiPackage>
                <modelPackage>com.model.profile</modelPackage>
                <invokerPackage>com.client.profile</invokerPackage>
            </configuration>
        </execution>
    </executions>
</plugin>

这会生成类似这样的类:

@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2018-07-25T16:16:45.852-04:00")
public class Identifiers {
   @SerializedName("Identifier")
   private String identifier = null;
   @SerializedName("Addresses")
   private List<Address> addresses = null;

但是,当我返回JSON文件时,它会这样返回:

[
    {
        "identifier": "66665455",
        "addresses": [
            {
                "street": "4785 North Gate Rd",
                "city": "CROSS LANES",
                "state": "NV",
                "postalCode": "88888"
            },
            {
                "street": "112 FOXRIDGE RUN",
                "city": "LONGWOOD",
                "state": "FL",
                "postalCode": "32750"
            }
        ]
    }
]

是否有一种方法可以强制 jackson 进行这些 MISMO 命名转换,就像在@SerializedName中那样? 如下所示:

[
    {
        "Identifier": "66665455",
        "Addresses": [
            {
                "Street": "4785 North Gate Rd",
                "State": "CROSS LANES",
                "state": "NV",
                "PostalCode": "88888"
            },
            {
                "Street": "112 FOXRIDGE RUN",
                "City": "LONGWOOD",
                "State": "FL",
                "PostalCode": "32750"
            }
        ]
    }
]

yaml:

...
Address:
    type: object
    properties:
      Street:
        description: The street number and street name of the address
        type: string
        example: 601 Riverside Ave
      City:
        description: The city portion of the address
        type: string
        example: Jacksonville
      State:
        description: The state portion of the address
        type: string
        example: FL
      PostalCode:
        description: Zip code portion of the address
        type: string
        example: 32081
  Loan:
    type: object
    properties:
      Identifier:
        description: Loan Number for the loan
        type: string
        example: 001891465131
        format: string
      Addresses:
        type: array
        items:
          $ref: '#/definitions/Address'

0 个答案:

没有答案