项目场景:
项目场景:使用easypoi进行导出excel,excel导出报错java.lang.IllegalArgumentException: object is not an instance of declaring class,object is not an instance of declaring class
问题描述
根据报错信息,我们知道这是非法字符异常-对象没有一个反射实例,下面是我excel对象的描述: 思考片刻我就在想,我tm是不是data失效或者list这个地方赋值属性的时候用到了,后来想也没有啊
@ExcelTarget("questionContrastExcelVo")
@Data
public class QuestionContrastExcelVo implements IExcelDataModel, IExcelModel {
@TableField(exist = false)
private Integer rowNum;
@TableField(exist = false)
private String errorMsg;
private Long id;
@Excel(name = "序号",orderNum = "1",needMerge = true)
private Long iseq;
@Excel(name = "测试题标题",orderNum = "2",width = 75,needMerge = true)
@NotBlank(message = "[问题标题]不能为空")
private String itemTitle;
@Excel(name = "题型",orderNum = "3",width = 25,needMerge = true)
@NotBlank(message = "[问题标题]不能为空")
private String label;
@ExcelCollection(name = "选项详情",orderNum = "4")
private List<OptionsContrastExcelVo> optionsContrastVoList;
@ExcelCollection(name = "回答详情",orderNum = "5")
private List<AnswerContrastExcelVo> answerList;
}
原因分析:
遇事不决,先看报错信息;看不出名堂,就打断点看对象属性。 报错信息有这么一行进入了我的视线:
OptionsContrastVo这个对象,是我对象拷贝之前的对象啊,拷贝后的对象应该是OptionsContrastExcelVo呀,难道…
是的,在这个我使用beanutils进行拷贝是,是引用地址拷贝,所以,对象并非我希望的,重新生成的OptionsContrastExcelVo。所以,OptionsContrastVo字段上并没有@excelcollection标签。所以造成了上述反射报错。
知识盲区
在对象拷贝中,拷贝方式分为两种,一种是地址引用式的浅拷贝,和重新生成对象的深拷贝方式,他们区别如下:
1.Apache Commons Lang序列化方式深拷贝
package com.lyj.demo.pojo.cloneTest;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
@Getter
@Setter
public class AddressSerializable implements Serializable {
private String address1;
private String address2;
public AddressSerializable() {
}
public AddressSerializable(String address1, String address2) {
this.address1 = address1;
this.address2 = address2;
}
}
package com.lyj.demo.pojo.cloneTest;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.SerializationUtils;
import java.io.Serializable;
@Getter
@Setter
public class UserSerializable implements Serializable {
private String userName;
private AddressSerializable address;
public UserSerializable() {
}
public UserSerializable(String userName, AddressSerializable address) {
this.userName = userName;
this.address = address;
}
public static void main(String[] args) {
AddressSerializable address = new AddressSerializable("小区1", "小区2");
UserSerializable user = new UserSerializable("小李", address);
UserSerializable copyUser = SerializationUtils.clone(user);
user.getAddress().setAddress1("小区3");
System.out.println(user == copyUser);
System.out.println(user.getAddress().getAddress1().equals(copyUser.getAddress().getAddress1()));
}
}
2.Gson序列化方式深拷贝
Gson可以将对象序列化成JSON,也可以将JSON反序列化成对象,所以我们可以用它进行深拷贝。
package com.lyj.demo.pojo.cloneTest;
import lombok.Data;
@Data
public class AddressGson {
private String address1;
private String address2;
public AddressGson() {
}
public AddressGson(String address1, String address2) {
this.address1 = address1;
this.address2 = address2;
}
}
package com.lyj.demo.pojo.cloneTest;
import com.google.gson.Gson;
import lombok.Data;
@Data
public class UserGson {
private String userName;
private AddressGson address;
public UserGson() {
}
public UserGson(String userName, AddressGson address) {
this.userName = userName;
this.address = address;
}
public static void main(String[] args) {
AddressGson address = new AddressGson("小区1", "小区2");
UserGson user = new UserGson("小李", address);
Gson gson = new Gson();
UserGson copyUser = gson.fromJson(gson.toJson(user), UserGson.class);
user.getAddress().setAddress1("小区3");
System.out.println(user == copyUser);
System.out.println(user.getAddress().getAddress1().equals(copyUser.getAddress().getAddress1()));
}
}
3.jackson序列化方式
Jackson与Gson相似,可以将对象序列化成JSON,明显不同的地方是拷贝的类(包括其成员变量)需要有默认的无参构造函数。
package com.lyj.demo.pojo.cloneTest;
import lombok.Data;
@Data
public class AddressJackson {
private String address1;
private String address2;
public AddressJackson() {
}
public AddressJackson(String address1, String address2) {
this.address1 = address1;
this.address2 = address2;
}
}
package com.lyj.demo.pojo.cloneTest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
@Data
public class UserJackson {
private String userName;
private AddressJackson address;
public UserJackson() {
}
public UserJackson(String userName, AddressJackson address) {
this.userName = userName;
this.address = address;
}
public static void main(String[] args) throws JsonProcessingException {
AddressJackson address = new AddressJackson("小区1", "小区2");
UserJackson user = new UserJackson("小李", address);
ObjectMapper objectMapper = new ObjectMapper();
UserJackson copyUser = objectMapper.readValue(objectMapper.writeValueAsString(user), UserJackson.class);
user.getAddress().setAddress1("小区3");
System.out.println(user == copyUser);
System.out.println(user.getAddress().getAddress1().equals(copyUser.getAddress().getAddress1()));
}
}
上述三种都是常用的,针对于单个对象好用,如果拷贝是list对象,那么有点麻烦。所以我最后选用的是api居多的fastjson来进行序列化转换的
解决方案:
analyseExcelList = JSON.parseArray(JSON.toJSONString(analyse), QuestionContrastExcelVo.class);
FASTJSON支持数组方式,这是很方便的,而像gson需要进一步的转化
Gson gson = new Gson();
QuestionContrastExcelVo[] questionContrastExcelVos = gson.fromJson(gson.toJson(analyse), QuestionContrastExcelVo[].class);
Collections.addAll(analyseExcelList,questionContrastExcelVos);
|