如何使用Lombok + Gson创建JSON对象数组?

mm5n2pyu  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(202)

说明:我使用Lombok的@Builder注解来“构建”一个JSON有效负载,然后使用gson将其转换为正确的JSON输出。

但是如何通过builder()方法构建数组呢?

代码:

RoleType RoleType = RoleType.getEnumByUserRole("Marketing");
        PropertyBean PropertyBean = ConfigFactory.create(PropertyBean.class);
        String defaultStore = "null";

        //this is the object that's suppose to be an array.
        Group group = Group.builder()
                .groupId(RoleType.getGroupId())
                .changedById(PropertyBean.sssUser())
                .storeCode(defaultStore)
                .primary(false).build();

        String lastName = "QA User";
        int numOfDays = 1;
        String defaultLocale = "en";
        User newUser = User.builder()
                .firstName(RoleType.getAcronym())
                .lastName(lastName)
                .startDay(getCurrentYearMonthDate())
                .endDay(addDaysToYearMonthDate(numOfDays))
                .password(PropertyBean.tempPass())
                .rightHand(true)
                .operatorId("123456")
                .userStores(userGroup) //<--- the userStoes object should be an array.
                .locale(defaultLocale).build();

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String body = gson.toJson(newUser);

        System.out.println(body);

输出量:

{
  "firstName": "MKT_AUTO",
  "lastName": "QA User",
  "locale": "en",
  "rightHand": true,
  "startDay": "2020-02-03",
  "endDay": "2020-02-04",
  "operatorId": "123456",
  "password": "Temp-Auto01",
  "global": false,
  "userStores": {
    "storeCode": "null",
    "groupId": 24,
    "changedById": "000000081",
    "primary": false
  },
  "active": false,
  "lockedOutFlag": false
}

所需输出

{
  "firstName": "MKT_AUTO",
  "lastName": "QA User",
  "locale": "en",
  "rightHand": true,
  "startDay": "2020-02-03",
  "endDay": "2020-02-04",
  "operatorId": "81",
  "password": "Temp-Auto01",
  "global": false,
  "userStores": [{ //<---- Array
    "storeCode": "null",
    "groupId": 24,
    "changedById": "000000081",
    "primary": false
  }],
  "active": false,
  "lockedOutFlag": false
}
oxcyiej7

oxcyiej71#

在列表类型中省略builder的用法是可行的,然后只使用ArrayList<>()方法就可以了。
示例:

List<Group> userStores = new ArrayList<>();
        userStores.add(new Group("216", 1, "81", true));

        String lastName = "QA User";
        int numOfDays = 1;
        String defaultLocale = "en";
        User newUser = User.builder()
                .firstName(RoleType.getAcronym())
                .lastName(lastName)
                .startDay(getCurrentYearMonthDate())
                .endDay(addDaysToYearMonthDate(numOfDays))
                .password(PropertyBean.tempPass())
                .rightHand(true)
                .operatorId("123456")
                .userStores(userStores)
                .locale(defaultLocale).build();

输出量:

{
  "firstName": "MKT_AUTO",
  "lastName": "QA User",
  "locale": "en",
  "rightHand": true,
  "startDay": "2020-02-03",
  "endDay": "2020-02-04",
  "operatorId": "123456",
  "password": "Temp-Auto01",
  "global": false,
  "userStores": [
    {
      "storeCode": "216",
      "groupId": 1,
      "changedById": "81",
      "primary": true
    }
  ],
  "active": false,
  "lockedOutFlag": false
}
ie3xauqp

ie3xauqp2#

我知道这是一个老问题,可能不再有什么不同,但是集合可以很好地与Lombok的构建器语法一起工作。

import lombok.Builder;

@Builder
public class TestBuilder {

  private final List<String> testStrings;

}

这样,您就可以调用TestBuilder.builder().testStrings(List.of("test", "test2")).build();而不会出现任何问题。
您也可以加入@Singular注解,以实作.add函式,将单一对象加入数组。
Baeldung上可以找到一个很好的例子。
由于OP没有显示他的lombok的源代码,我所能做的就是User类不期望userStores的集合,因为发送了一个组。

相关问题