问题描述:
com.alibaba.fastjson.JSONException: parseInt error, field : id
问题分析:
1、使用JSON.parseArray()进行实体类类型转换时,UserEntity的id属性类型为Long,而UserDto的id属性类型为Integer,导致UserEntity转换为UserDto时,Long类型转换不了Integer类型,导致报错。
List<UserEntity> userEntityList = new ArrayList<>(Arrays.asList(new UserEntity(1432957941805654017L)));
List<UserDto> userDtoList = JSON.parseArray(JSON.toJSONString(userEntityList, SerializerFeature.WriteMapNullValue), UserDto.class);
package com.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserEntity {
private Long id;
}
package com.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class UserDto {
private Integer id;
}
解决办法:
两个实体类对应的属性类型一致即可。