0

I have the following Rest controller method:

@GetMapping
public Page<CompanyDto> findAllCompanies(@RequestParam(value = "name", required = false) String name, Pageable pageable, JwtAuthenticationToken jwtAuthenticationToken) {
...

and the Feigh client:

@GetMapping
RestPageImpl<CompanyDto> findAllCompanies(@RequestParam(value = "name", required = false) String name, Pageable pageable, @RequestHeader("Authorization") String token);

So far everything works fine.

Now, I'd like to substitute name and pageable parameters with a single DTO object:

public class CompanyRequest {

    private CompanyDto company;

    Pageable pageable;

    public CompanyRequest() {
    }

    public CompanyRequest(CompanyDto company, Pageable pageable) {
        this.company = company;
        this.pageable = pageable;
    }

    public CompanyDto getCompany() {
        return company;
    }

    public Pageable getPageable() {
        return pageable;
    }
    
}

to something like this:

controller:

@GetMapping
public Page<CompanyDto> findAllCompanies(CompanyRequest companyRequest, JwtAuthenticationToken jwtAuthenticationToken) {
...

Feign client:

@GetMapping
RestPageImpl<CompanyDto> findAllCompanies(CompanyRequest companyRequest, @RequestHeader("Authorization") String token);

Right now the invocation of the following code:

companyApiClient.findAllCompanies(new CompanyRequest(new CompanyDto("Company1 name", null), PageRequest.of(0, 10)), accessToken);

fails with the following exception:

Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.decisionwanted.api.model.dto.page.RestPageImpl`, problem: Page size must not be less than one!; nested exception is com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of `com.decisionwanted.api.model.dto.page.RestPageImpl`, problem: Page size must not be less than one!
 at [Source: (PushbackInputStream); line: 1, column: 1499]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:389)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:342)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:105)
    ... 42 more
Caused by: com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of `com.decisionwanted.api.model.dto.page.RestPageImpl`, problem: Page size must not be less than one!
 at [Source: (PushbackInputStream); line: 1, column: 1499]
    at com.fasterxml.jackson.databind.exc.ValueInstantiationException.from(ValueInstantiationException.java:47)
    at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1754)
    at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.wrapAsJsonMappingException(StdValueInstantiator.java:491)
    at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.rewrapCtorProblem(StdValueInstantiator.java:514)
    at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:285)
    at com.fasterxml.jackson.databind.deser.ValueInstantiator.createFromObjectWith(ValueInstantiator.java:229)
    at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:202)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:490)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1310)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:331)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:164)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4482)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3487)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:378)
    ... 44 more
Caused by: java.lang.IllegalArgumentException: Page size must not be less than one!
    at org.springframework.data.domain.AbstractPageRequest.<init>(AbstractPageRequest.java:48)
    at org.springframework.data.domain.PageRequest.<init>(PageRequest.java:45)
    at org.springframework.data.domain.PageRequest.of(PageRequest.java:72)
    at org.springframework.data.domain.PageRequest.of(PageRequest.java:60)
    at com.decisionwanted.api.model.dto.page.RestPageImpl.<init>(RestPageImpl.java:27)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at com.fasterxml.jackson.databind.introspect.AnnotatedConstructor.call(AnnotatedConstructor.java:124)
    at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:283)
    ... 53 more

What am I doing wrong and how to fix it?

UPDATED

This is RestPageImpl:

public class RestPageImpl<T> extends PageImpl<T> {

    @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
    public RestPageImpl(@JsonProperty("content") List<T> content,
                        @JsonProperty("number") int number,
                        @JsonProperty("size") int size,
                        @JsonProperty("totalElements") Long totalElements,
                        @JsonProperty("pageable") JsonNode pageable,
                        @JsonProperty("last") boolean last,
                        @JsonProperty("totalPages") int totalPages,
                        @JsonProperty("sort") JsonNode sort,
                        @JsonProperty("first") boolean first,
                        @JsonProperty("numberOfElements") int numberOfElements) {

        super(content, PageRequest.of(number, size), totalElements);
    }

    public RestPageImpl(List<T> content, Pageable pageable, long total) {
        super(content, pageable, total);
    }

    public RestPageImpl(List<T> content) {
        super(content);
    }

    public RestPageImpl() {
        super(new ArrayList<>());
    }

}
alexanoid
  • 24,051
  • 54
  • 210
  • 410
  • Please also include `RestPageImpl` as the error is thrown from its constructor. – samabcde Jun 23 '21 at 14:58
  • sure, done. Please see updated question – alexanoid Jun 23 '21 at 15:01
  • Where does `@JsonProperty("number")`, `@JsonProperty("size")` come from? Can you check the value of `size`? This value causes the error. – samabcde Jun 23 '21 at 15:14
  • I execute the following code in my test: `Page companyDtoPage = companyApiClient.findAllCompanies(new CompanyRequest(new CompanyDto("Company1 name", null)), PageRequest.of(0, 10), accessToken);` – alexanoid Jun 23 '21 at 15:18
  • I mean how the service provide response to `companyApiClient.findAllCompanies`, the error is thrown on `RestPageImpl` line `super(content, PageRequest.of(number, size), totalElements);`, meaning the value of `size` has problem. Try to log the json response from feign to see what is wrong. – samabcde Jun 23 '21 at 15:26
  • It is even doesn't invoke `findAllCompanies` controller method.. Probably Feign sees the `RestPageImpl` in method declaration and does some magic under the hood.. – alexanoid Jun 23 '21 at 15:28

0 Answers0