我正在尝试使用beanio将带注释的类写入fixedlength文件。所有课程都已注释,但我得到了异常
“无效字段'员工',在记录'团队'中,在流'Tm'中:找不到类型'com.mycompany.bio.Employee'的类型处理程序”
以下是我的源代码
public static void main(String[] args) {
StreamFactory factory = StreamFactory.newInstance();
StreamBuilder builder = new StreamBuilder("Tm")
.format("fixedlength")
.parser(new FixedLengthParserBuilder())
.addRecord(com.mycompany.bio.Team.class);
factory.define(builder);
Employee e1 = new Employee("EmpF1", "EmpL1", "Developer", "1", new Date());
Employee e2 = new Employee("EmpF2", "EmpL2", "Developer", "2", new Date());
Team team = new Team();
team.setTeamName("Great Team");
team.getEmployees().add(e1);
team.getEmployees().add(e2);
BeanWriter out = factory.createWriter("Tm", new File("C:\\Users\\user\\Desktop\\tm.dat"));
out.write(team);
out.flush();
out.close();
}
团队课程:
@Record(minOccurs = 1)
public class Team {
// @Segment(collection = ArrayList.class, minOccurs = 0, maxOccurs = Integer.MAX_VALUE) segments return single line team::teamName and 2 emp records, I want to see 3 lines 1.teamname 2 & 3 emp info
@Field(ordinal = 1, length = 106)
private List<Employee> employees;
@Field(ordinal = 0, length = 10)
private String teamName;
.....
}
员工类:
@Record(minOccurs=1)
public class Employee {
@Field(ordinal = 1, length = 30)
private String firstName;
@Field(ordinal = 2, length = 30)
private String lastName;
@Field(ordinal = 3, length = 30)
private String title;
@Field(ordinal = 4, length = 8)
private String salary;
@Field(ordinal = 5, format="MMddyyyy", length = 8)
private Date hireDate;
...
}
答案 0 :(得分:4)
快速解决方案是:
@Record
public class Team {
@Field(ordinal = 1, length = 10)
private String teamName;
private List<Employee> employees = new ArrayList<>();
....}
团队课程:
@Record
public class Employee {
@Field(ordinal = 1, length = 30)
private String firstName;
@Field(ordinal = 2, length = 30)
private String lastName;
@Field(ordinal = 3, length = 30)
private String title;
@Field(ordinal = 4, length = 8)
private String salary;
@Field(ordinal = 5, format="MMddyyyy", length = 8)
private Date hireDate;
....}
员工:
Great Team
EmpF1 EmpL1 Developer 1 20150622
EmpF2 EmpL2 Developer 2 20150622
输出文件内容:
WKInterfacePicker