EasyPoi导出异常 - java.lang.IllegalArgumentException: object is not an instance of declaring class
项目场景:项目场景:使用easypoi进行导出excel,excel导出报错java.lang.IllegalArgumentException: object is not an instance of declaring class,object is not an instance of declaring class问题描述根据报错信息,我们知道这是非法字符异常-对象没有一个反射实例,下面
项目场景:
项目场景:使用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) //needMerge合并单元格
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;
/**
* @date 2021/4/15 15:11
*/
@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;
/**
* 通过Apache Commons Lang 序列化方式深拷贝
* Java提供了序列化的能力,我们可以先将源对象进行序列化,再反序列化生成拷贝对象。
* 但是,使用序列化的前提是拷贝的类(包括其成员变量)需要实现Serializable接口。
* Apache Commons Lang包对Java序列化进行了封装,我们可以直接使用它。
*/
@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");
// false
System.out.println(user == copyUser);
// false
System.out.println(user.getAddress().getAddress1().equals(copyUser.getAddress().getAddress1()));
}
}
2.Gson序列化方式深拷贝
Gson可以将对象序列化成JSON,也可以将JSON反序列化成对象,所以我们可以用它进行深拷贝。
package com.lyj.demo.pojo.cloneTest;
import lombok.Data;
/**
* @date 2021/4/15 15:31
*/
@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;
/**
* @date 2021/4/15 15:30
* 使用Gson序列化方式进行深拷贝
* Gson可以将对象序列化成JSON,也可以将JSON反序列化成对象,所以我们可以用它进行深拷贝
*/
@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 gson = new Gson();
UserGson copyUser = gson.fromJson(gson.toJson(user), UserGson.class);
user.getAddress().setAddress1("小区3");
// false
System.out.println(user == copyUser);
// false
System.out.println(user.getAddress().getAddress1().equals(copyUser.getAddress().getAddress1()));
}
}
3.jackson序列化方式
Jackson与Gson相似,可以将对象序列化成JSON,明显不同的地方是拷贝的类(包括其成员变量)需要有默认的无参构造函数。
package com.lyj.demo.pojo.cloneTest;
import lombok.Data;
/**
* @author 凌兮
* @date 2021/4/15 15:41
*/
@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;
/**
* @date 2021/4/15 15:40
* 通过Jackson方式实现深拷贝
* Jackson与Gson相似,可以将对象序列化成JSON,明显不同的地方是拷贝的类(包括其成员变量)需要有默认的无参构造函数。
*/
@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);
// 使用Jackson序列化进行深拷贝
ObjectMapper objectMapper = new ObjectMapper();
UserJackson copyUser = objectMapper.readValue(objectMapper.writeValueAsString(user), UserJackson.class);
user.getAddress().setAddress1("小区3");
// false
System.out.println(user == copyUser);
// false
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);
更多推荐
所有评论(0)