V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tiRolin
V2EX  ›  Java

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

  •  
  •   tiRolin · 2023-01-20 21:02:59 +08:00 · 1137 次点击
    这是一个创建于 433 天前的主题,其中的信息可能已经有所发展或是发生改变。

    项目需求是要实现同乡会的搜索返回功能,使用 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> {
    
    }
    
    4 条回复    2023-01-20 22:29:17 +08:00
    tiRolin
        1
    tiRolin  
    OP
       2023-01-20 21:12:27 +08:00
    这个问题困扰我很久了,我能找到的解决方法都去试了,还是解决不了所以来请教各位,有懂的大佬可以救一下
    yangyaofei
        2
    yangyaofei  
       2023-01-20 21:16:59 +08:00
    应该有具体报错, 告诉你为啥不行

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

    还有就是, 拿一条数据试试, 没准拿一条数据(或者特定的数据)没问题, 那就不是反序列化的问题, 可能就是 es 里面的数据有问题了
    tiRolin
        3
    tiRolin  
    OP
       2023-01-20 22:02:10 +08:00
    @yangyaofei 谢谢你,经过你的提示,我往实体类里的 LocalDate 属性上加了
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    这三个注解,最终成功解决了这个问题,真的是太感谢你了
    yangyaofei
        4
    yangyaofei  
       2023-01-20 22:29:17 +08:00
    @tiRolin 没帮上什么, 但是看你的回复, 用的是比较早的 spring data es 版本了, 现在好像这样就不行了(或者说更简单了)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5622 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 06:33 · PVG 14:33 · LAX 23:33 · JFK 02:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.