【问题标题】:Elasticsearch "org.springframework.http.converter.HttpMessageNotWritableException"弹性搜索“org.springframework.http.converter.HttpMessageNotWritableException”
【发布时间】:2019-02-01 12:13:51
【问题描述】:

我正在尝试运行这个简单的 spring boot - elasticsearch 应用程序,但是当我 GET /localhost:8080/findAll 时出现此错误

{
"timestamp": 1549021796136,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.http.converter.HttpMessageNotWritableException",
"message": "Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl[\"facets\"])",
"path": "/findAll"}

我有非常简单的类和接口:

Customer.java

package com.example.demogradleNew.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Document(indexName = "christouandr", type = "customer", shards = 2)
public class Customer {

    @Id
    private String id;
    private String firstName;
    private String lastName;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

CustomerRepository.java

@Repository
public interface CustomerRepository extends ElasticsearchRepository<Customer, String> {
    List<Customer> findByFirstName(String firstName);
}

DemogradleNewApplication.java

package com.example.demogradleNew;

import com.example.demogradleNew.model.Customer;
import com.example.demogradleNew.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@SpringBootApplication
@RestController
public class DemogradleNewApplication {

    @Autowired
    private CustomerRepository repository;

    @GetMapping("/findAll")
    public Iterable<Customer> findAllCustomers() {
        return repository.findAll();
    }

    @GetMapping("/findByName/{firstName}")
    public List<Customer> findByName(@PathVariable String firstName){
        return repository.findByFirstName(firstName);
    }

    @PostMapping("/saveCustomer")
    public int saveCustomer(@RequestBody List<Customer> customers){
        repository.save(customers);
        return customers.size();
    }


    public static void main(String[] args) {
        SpringApplication.run(DemogradleNewApplication.class, args);
    }

}

我的 build.gradle 文件如下:

buildscript {
    ext {
        springBootVersion = '1.5.19.BUILD-SNAPSHOT'
    }
    repositories {
        mavenCentral()
        maven { url 'https://repo.spring.io/snapshot' }
        maven { url 'https://repo.spring.io/milestone' }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'eclipse'


group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/snapshot' }
    maven { url 'https://repo.spring.io/milestone' }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    runtimeOnly 'org.springframework.boot:spring-boot-devtools'
    compileOnly 'org.projectlombok:lombok' 
}

我尝试了这篇文章所说的一切,但仍然遇到同样的错误。当我使用 Maven 时,我的代码运行良好。但是当我使用 Gradle 时,我得到了这个错误。 我一直在寻找几个小时来解决这个问题,但我还没有找到答案。谢谢!

【问题讨论】:

  • 我尝试了这篇文章所说的一切,但我仍然遇到同样的错误。当我使用 Maven 时,我的代码运行良好。但是当我使用 Gradle 时,我得到了这个错误。
  • 可能有版本冲突?也很高兴看到您的 Maven 配置(pom.xml)
  • 只需将Iterable&lt;Customer&gt; 更改为List&lt;Customer&gt;,这适用于所有JPA 数据,而不是仅适用于ES
  • 好吧,我找到了答案,答案如下!谢谢你的帮助!

标签: java spring spring-boot elasticsearch jackson-databind


【解决方案1】:

好的,我找到了答案! 令人惊讶的是,将其添加到 DemogradleNewApplication.java 解决了问题!

import com.google.common.collect.Lists;

public List<Customer> findAll() {
    return Lists.newArrayList(repository.findAll());
}

所以,最终的 DemogradleNewApplication.java 文件如下!

package com.example.demogradleNew;

import com.example.demogradleNew.model.Customer;
import com.example.demogradleNew.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.google.common.collect.Lists;


@SpringBootApplication
@RestController
public class DemogradleNewApplication {

    @Autowired
    private CustomerRepository repository;

    @GetMapping("/findAll")
    public List<Customer> findAll() {
        return Lists.newArrayList(repository.findAll());
    }
    @GetMapping("/findByName/{firstName}")
    public List<Customer> findByName(@PathVariable String firstName){
        return repository.findByFirstName(firstName);
    }

    @PostMapping("/saveCustomer")
    public int saveCustomer(@RequestBody List<Customer> customers){
        repository.save(customers);
        return customers.size();
    }


    public static void main(String[] args) {
        SpringApplication.run(DemogradleNewApplication.class, args);
    }

}

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 2020-03-29
    • 1970-01-01
    • 2022-12-08
    • 1970-01-01
    相关资源
    最近更新 更多