问题:fastjson反序列化遇到坑(无法对返回值不为void的set方法赋值???求解) static class A{ private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "A{" + "id=" + id + ", name='" + name + '\'' + '}'; } } static class B<M extends Model<M>> extends Model<M> { public Integer getId() { return get("id"); } public M setId(Integer id) { return set("id", id); } public String getName() { return get("name"); } public void setName(String name) { set("name", name); } @Override public String toString() { return "B{" + "id=" + this.getId() + ", name='" + this.getName() + '\'' + '}'; } } static class BB extends B<BB>{ } @org.junit.Test public void test3(){ String rawData = "[{id: 1, name: '123'}, {id: 2, name: '456'}]"; List<A> a = JSON.parseArray(rawData, A.class); System.out.println(a); List<B> b = JSON.parseArray(rawData, B.class); System.out.println(b); List<BB> bb = JSON.parseArray(rawData, BB.class); System.out.println(bb); }
输出结果:
项目:JFinal
这也是为啥 jfinal 的 BaseModel 生成器所生成的 setter 方法没有返回值的原因
建议你找找 fastjson 的文档,估计有相关配置可以支持