
项目需求是要实现同乡会的搜索返回功能,使用 Spring Data Elasticsearch 完成,在
但是 Controller 层调用对应的实体的查询所有方法时报 failed to map source[entity data]to xxx class 异常,去百度上查找得到的解决方案都是在实体类中提供空参构造器,但是我已经提供了
下面是代码,Controller 层,发生错误的方法是 findAll()
@GetMapping("test") public void test(){ List<Village> list = villageService.list(); villageDao.saveAll(list); Iterable<Village> all = villageDao.findAll(); for (Village village : all) { System.out.println(village); } } Village 实体类
@Getter @Setter @TableName("v_village") @ApiModel(value = "Village 对象", description = "") @Document(indexName = "village") @Data @ToString @AllArgsConstructor @NoArgsConstructor public class Village implements Serializable { private static final long serialVersiOnUID= 1L; @Id @TableId(value = "id", type = IdType.ASSIGN_ID) @Field(index = false,type = FieldType.Keyword) private String id; @ApiModelProperty("同乡会头像") @Field(index = false,type = FieldType.Text) private String avatarUrl; @ApiModelProperty("学校 Id ,对应 a_university") @Field(index = false,type = FieldType.Keyword) private Integer unId; @ApiModelProperty("名称") @Field(type = FieldType.Text) private String name; @ApiModelProperty("负责人微信") @Field(index = false,type = FieldType.Text) private String wechat; @ApiModelProperty("负责人姓名") @Field(type = FieldType.Text) private String master; @ApiModelProperty("公众号文章链接") @Field(index = false,type = FieldType.Text) private String articleUrl; @ApiModelProperty("二维码") @Field(index = false,type = FieldType.Text) private String qrCode; @ApiModelProperty("点赞数") @Field(index = false,type = FieldType.Integer) private Integer likeNum; @ApiModelProperty("排序,小的在前") @Field(index = false,type = FieldType.Integer) private Integer sort; @ApiModelProperty("省 id") @Field(type = FieldType.Keyword) private String provinceId; @ApiModelProperty("城市 Id") @Field(type = FieldType.Keyword) private String cityId; @ApiModelProperty("区 Id") @Field(type = FieldType.Keyword) private String districtId; @Field(index = false,type = FieldType.Integer) private Integer isDeleted; @TableField(fill = FieldFill.INSERT) @Field(index = false,type = FieldType.Date) private LocalDateTime createTime; @TableField(fill = FieldFill.INSERT_UPDATE) @Field(index = false,type = FieldType.Date) private LocalDateTime updateTime; } 按照 ES 规范构造的 Repository 接口
public interface VillageDao extends ElasticsearchCrudRepository<Village,String> { } 1 tiRolin OP 这个问题困扰我很久了,我能找到的解决方法都去试了,还是解决不了所以来请教各位,有懂的大佬可以救一下 |
2 yangyaofei 2023-01-20 21:16:59 +08:00 应该有具体报错, 告诉你为啥不行 还有, entity 类的构造器可以加上 spring data 的注解, 指示用他进行反序列化 还有就是, 拿一条数据试试, 没准拿一条数据(或者特定的数据)没问题, 那就不是反序列化的问题, 可能就是 es 里面的数据有问题了 |
3 tiRolin OP @yangyaofei 谢谢你,经过你的提示,我往实体类里的 LocalDate 属性上加了 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonDeserialize(using = LocalDateTimeDeserializer.class) @JsonSerialize(using = LocalDateTimeSerializer.class) 这三个注解,最终成功解决了这个问题,真的是太感谢你了 |