Elasticsearch "org.springframework.http.converter.HttpMessageNotWritableException" - PullRequest
0 голосов
/ 01 февраля 2019

Я пытаюсь запустить это простое приложение весенней загрузки --asticsearch, но я получаю эту ошибку при получении / 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, я получаю эту ошибку.Я часами искал решение этой проблемы, но пока не нашел ответа.Спасибо!

1 Ответ

0 голосов
/ 01 февраля 2019

Хорошо, ребята, я нашел ответ!Удивительно, но добавление этого в 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);
    }

}
...