项目中使用 Spring Data Elasticsearch 出现 failed to map source[entity data]to xxx class,求解 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tiRolin
V2EX    Java

项目中使用 Spring Data Elasticsearch 出现 failed to map source[entity data]to xxx class,求解

  •  
  •   tiRolin 2023 年 1 月 20 日 1752 次点击
    这是一个创建于 1089 天前的主题,其中的信息可能已经有所发展或是发生改变。

    但是 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> { } 
    4 条回复    2023-01-20 22:29:17 +08:00
    tiRolin
        1
    tiRolin  
    OP
       2023 年 1 月 20 日
    这个问题困扰我很久了,我能找到的解决方法都去试了,还是解决不了所以来请教各位,有懂的大佬可以救一下
    yangyaofei
        2
    yangyaofei  
       2023 年 1 月 20 日
    应该有具体报错, 告诉你为啥不行

    还有, entity 类的构造器可以加上 spring data 的注解, 指示用他进行反序列化

    还有就是, 拿一条数据试试, 没准拿一条数据(或者特定的数据)没问题, 那就不是反序列化的问题, 可能就是 es 里面的数据有问题了
    tiRolin
        3
    tiRolin  
    OP
       2023 年 1 月 20 日
    @yangyaofei 谢谢你,经过你的提示,我往实体类里的 LocalDate 属性上加了
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    这三个注解,最终成功解决了这个问题,真的是太感谢你了
    yangyaofei
        4
    yangyaofei  
       2023 年 1 月 20 日
    @tiRolin 没帮上什么, 但是看你的回复, 用的是比较早的 spring data es 版本了, 现在好像这样就不行了(或者说更简单了)
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5610 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 39ms UTC 08:43 PVG 16:43 LAX 00:43 JFK 03:43
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86