ES8 查询报错:jakarta.json.JsonException: Jackson exception (JSON path: _source) (line no=1.......

报错内容

co.elastic.clients.json.JsonpMappingException: Error deserializing co.elastic.clients.elasticsearch.core.GetResponse: jakarta.json.JsonException: Jackson exception (JSON path: _source) (line no=1, column no=105, offset=-1)

。。。

 代码:

        //用JSONObject接收,正常
        GetResponse<JSONObject> response = client.get(s -> s
                        .index("test")
                        .id("1")
                ,
                JSONObject.class
        );
        JSONObject source = response.source();
        //用实体类接收,这里报错
        GetResponse<EsDemo> response2 = client.get(s -> s
                        .index("test")
                        .id("1")
                ,
                EsDemo.class
        );
原因:

新增数据时使用了 Spring Data Elasticsearch,会多出_class,但实体类并没有这个属性

两种解决方案:

1. 接受的实体类加入注解 @JsonIgnoreProperties(ignoreUnknown = true)

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@Document(indexName = "test")
public class EsDemo {

    @Id()
    private Long id;
    @Field(type = FieldType.Integer)
    private Integer userId;
    @Field(type = FieldType.Text)
    private String userName;

}

2. 新增时不使用  Spring Data Elasticsearch,使用 Elasticsearch 官方提供的高级客户端库 - Elasticsearch Api Client

        //Spring Data Elasticsearch 新增方式
        EsDemo esDemo = new EsDemo();
        esDemo.setId(2l);
        esDemo.setUserId(123456);
        esDemo.setUserName("测试2");
        EsDemo save = EsDemoRepository.save(esDemo);
        //Elasticsearch Api Client 新增方式
        IndexResponse test = client.index(i -> i.index("test")
                        .id("2")
                        .document(esDemo));

 

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'es8Client'是指在创建名为'es8Client'的Bean时出现了异常。 根据引用和引用的描述,可能是因为在工程启动时实例化ClientDetailsService类Bean时出现了异常。这可能是由于Seata框架的引入导致的,它在工程启动时会扫描到该Bean的方法触发方法执行,但是没有找到对应的Bean,或者找到了一个虚假的Bean,从而导致异常抛出。 根据引用的描述,可能是因为默认方法无法创建ClientDetailsService类Bean。Spring Cloud Oauth2框架中创建ClientDetailsService类默认Bean的方法是一个“假方法”,执行后并无法创建ClientDetailsService类Bean,而是固定抛出操作不支持异常。建议使用内存或JDBC方式配置客户端。 解决方案是根据具体情况进行调试和修复。可能需要确认是否正确配置了Seata框架,并检查是否存在与'es8Client'相关的Bean定义或配置问题。还可以尝试使用内存或JDBC方式配置客户端来解决问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Spring Cloud【2】Oauth2与Seata冲突](https://blog.csdn.net/qq_39288456/article/details/122563541)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值