带注释的 beanIO - 将对象列表写入 json 文件
beanIO with annotations - writing list of objects to a json file
我必须使用 beanIO 将对象列表写入 json 文件。每当我尝试时,我只得到第一个对象被写入文件,如下所示。
{"employeeDetails":[{"recordType":"I","empId":"100","empName":"Name1"}]}
实际结果应该如下:
{"employeeDetails":[{"recordType":"I","empId":"100","empName":"Name1"},{"recordType":"I","empId":"101","empName":"Name2"}]}
使用 pojo 如下:
@Record
public class Employee{
@Field(minOccurs=0)
private String recordType;
@Field(minOccurs=0)
private String empId;
@Field(minOccurs=0)
private String empName;
// getters and setters
}
@Record
public class Department{
@Segment(minOccurs=0, collection=List.class)
private List<Employee> employeeDetails;
//getters and setters
}
这就是我的实现 class 所做的,
StreamFactory streamFactory=StreamFactory.newInstance();
streamFactory.loadResource(beanIoPath + beanIoMappingFileName);
Writer outJson = new BufferedWriter(new FileWriter(new File(absPath+fileName)));
BeanWriter jsonBeanWriter = streamFactory.createWriter(mapper, outJson);
Department dpt = //fetch from db;
jsonBeanWriter.write(dpt);
请建议应该添加更多内容,如何使用 BeanIO 将对象列表写入 json 文件。
谢谢..
问题出在 Department
class 的配置上。 Segment
上的 maxOccurs
属性的默认值为 Integer.MIN_VALUE
,因此如果要使用映射文件,则需要将其设置为 "unbounded",但您可以设置使用注释时将值设置为 -1
。来自 @Segment
注释的源代码:
/**
* The maximum occurrences.
* @return the maximum occurrences, or -1 if unbounded
*/
int maxOccurs() default Integer.MIN_VALUE;
您的 Department
class 需要如下所示:
@Record
public class Department {
@Segment(minOccurs = 0, maxOccurs = -1, collection = List.class)
private List<Employee> employeeDetails;
//getters and setters
}
当您使用注释并需要按特定顺序输出字段时,请注意有关注释的文档part:
When using annotations, it is strongly recommended to explicitly set
the position (using at) for all fields and segments. BeanIO does not
guarrantee the order in which annotated components are added to a
layout.
我必须使用 beanIO 将对象列表写入 json 文件。每当我尝试时,我只得到第一个对象被写入文件,如下所示。
{"employeeDetails":[{"recordType":"I","empId":"100","empName":"Name1"}]}
实际结果应该如下:
{"employeeDetails":[{"recordType":"I","empId":"100","empName":"Name1"},{"recordType":"I","empId":"101","empName":"Name2"}]}
使用 pojo 如下:
@Record
public class Employee{
@Field(minOccurs=0)
private String recordType;
@Field(minOccurs=0)
private String empId;
@Field(minOccurs=0)
private String empName;
// getters and setters
}
@Record
public class Department{
@Segment(minOccurs=0, collection=List.class)
private List<Employee> employeeDetails;
//getters and setters
}
这就是我的实现 class 所做的,
StreamFactory streamFactory=StreamFactory.newInstance();
streamFactory.loadResource(beanIoPath + beanIoMappingFileName);
Writer outJson = new BufferedWriter(new FileWriter(new File(absPath+fileName)));
BeanWriter jsonBeanWriter = streamFactory.createWriter(mapper, outJson);
Department dpt = //fetch from db;
jsonBeanWriter.write(dpt);
请建议应该添加更多内容,如何使用 BeanIO 将对象列表写入 json 文件。
谢谢..
问题出在 Department
class 的配置上。 Segment
上的 maxOccurs
属性的默认值为 Integer.MIN_VALUE
,因此如果要使用映射文件,则需要将其设置为 "unbounded",但您可以设置使用注释时将值设置为 -1
。来自 @Segment
注释的源代码:
/**
* The maximum occurrences.
* @return the maximum occurrences, or -1 if unbounded
*/
int maxOccurs() default Integer.MIN_VALUE;
您的 Department
class 需要如下所示:
@Record
public class Department {
@Segment(minOccurs = 0, maxOccurs = -1, collection = List.class)
private List<Employee> employeeDetails;
//getters and setters
}
当您使用注释并需要按特定顺序输出字段时,请注意有关注释的文档part:
When using annotations, it is strongly recommended to explicitly set the position (using at) for all fields and segments. BeanIO does not guarrantee the order in which annotated components are added to a layout.