Описание : я использую аннотацию 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
}