trjade
trjade

Reputation: 210

Spring boot - Rest Pagination return message HttpMessageNotWritableException

I tried to implement pagination for my api with Spring Boot 1.5.3 and Elasticsearch but it returned HttpMessageNotWritableException: could not write JSON document instead.

I thought that ElasticsearchRepository already provided paging and sorting method so I tried to used that then return pageable object to my controller. And I think the error occurs when the controller returns the result.

Here is the controller.

@GetMapping(value = "/client",
            params = { "page", "size" })
public Page<Client> getAllClient( @RequestParam("page") int page, @RequestParam("size") int size){
    return clientService.getAllClient(page, size);
}

The service.

public Page<Client> getAllClient(int page, int size) {
    Page<Client> resultPage = clientRepository.findAll(new PageRequest(0, 3));
    return resultPage;
}

The repository.

public interface ClientRepository extends 
ElasticsearchRepository<Client, String> {
    public Client findByName(String name);
}

I already used Lombok for my entity so it should not be the problem.

@Data
@Document(indexName = "customer", type = "client")
public class Client {

    @Id
    private String id;

    private String name;

    private String city;

    private String phone;
}

dependencies in pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.8</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

When I make the HTTP GET request to the url that mapped with the controller, the error log below appeared.

WARN 12616 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON document: (was java.lang.NullPointerException) (through reference chain: org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl["facets"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl["facets"])

I wonder if I forgot any configuration in my project.

Upvotes: 1

Views: 545

Answers (1)

trjade
trjade

Reputation: 210

I just found that after I changed to Spring Boot version 2.0.4 it works!

But I still wanted to know how to do it in version 1.5.3 though.

Upvotes: -1

Related Questions