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

2023-01-20 21:02:59 +08:00
 tiRolin

项目需求是要实现同乡会的搜索返回功能,使用 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> {

}
1162 次点击
所在节点    Java
4 条回复
tiRolin
2023-01-20 21:12:27 +08:00
这个问题困扰我很久了,我能找到的解决方法都去试了,还是解决不了所以来请教各位,有懂的大佬可以救一下
yangyaofei
2023-01-20 21:16:59 +08:00
应该有具体报错, 告诉你为啥不行

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

还有就是, 拿一条数据试试, 没准拿一条数据(或者特定的数据)没问题, 那就不是反序列化的问题, 可能就是 es 里面的数据有问题了
tiRolin
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
2023-01-20 22:29:17 +08:00
@tiRolin 没帮上什么, 但是看你的回复, 用的是比较早的 spring data es 版本了, 现在好像这样就不行了(或者说更简单了)

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/910086

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX