1 year ago

#388331

test-img

Katlock

How to get a Page in EntityExchangeResult from spring reactive route?

I have a spring reactive route that returns a Page of type objects. I am able to get the String representation of the Page in my integ test case. However, I am not able to get the Page type. The following is my integ test case that works:

 EntityExchangeResult<String> result = client.get().uri("/applications")
                .exchange().expectStatus().isOk().expectBody(String.class).returnResult();

 LOG.info("found page applications: {}", result.getResponseBody());

The response body prints the page content in string representation like:

found page applications: {"content":[{"id":"d64264b6-95eb-4a77-a9ac-af93ba6cb7b4","name":"Closed Circuit Application","deprecated":false,"description":"this is a closed circuit tv app"],"pageable":{"sort":{"empty":true,"sorted":false,"unsorted":true},"offset":0,"pageNumber":0,"pageSize":100,"paged":true,"unpaged":false},"last":true,"totalPages":1,"totalElements":1,"size":100,"number":0,"sort":{"empty":true,"sorted":false,"unsorted":true},"numberOfElements":1,"first":true,"empty":false}

However, I want to get Page type instead like following which is not working, giving errors:

 EntityExchangeResult<PageImpl> result = client.get().uri("/applications")
                .exchange().expectStatus().isOk().expectBody(PageImpl.class).returnResult();

 LOG.info("found page applications: {}", result.getResponseBody());

When I use PageImpl I get following errors:

org.springframework.core.codec.CodecException: Type definition error: [simple type, class org.springframework.data.domain.Pageable]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.data.domain.Pageable` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: (io.netty.buffer.ByteBufInputStream); line: 1, column: 362] (through reference chain: org.springframework.data.domain.PageImpl["pageable"])

The following is my business service that sets the Page type:

 public Mono<ServerResponse> getApplications(ServerRequest serverRequest) {
        LOG.info("getApplications");
        Pageable pageable = getPageable(serverRequest);

        return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).
                body(applicationBehavior.getApplications(pageable), Page.class)
                .onErrorResume(e -> ServerResponse.badRequest().body(BodyInserters
                        .fromValue(e.getMessage())));
    }

java

spring-webflux

reactive

0 Answers

Your Answer

Accepted video resources