使用 Pageable 字段测试端点时出现 JsonMappingException [英] JsonMappingException when testing endpoints with Pageable field

查看:23
本文介绍了使用 Pageable 字段测试端点时出现 JsonMappingException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用一个需要 Pageable 字段的端点:

I need to call an endpoint which expects a Pageable field:

@GetMapping
public Page<ProductDTO> listProducts(Pageable pageable) {
    return productService.findProducts(pageable); 
}

在我的测试中,我有这个代码:

In my test I have this code:

MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
parameters.add("page", String.valueOf(0));
URI url = defaultURI(port, "/products", parameters);

ParameterizedTypeReference<RestResponsePage<ProductDTO>> type = new ParameterizedTypeReference<RestResponsePage<ProductDTO>>() {};
ResponseEntity<RestResponsePage<ProductDTO>> response = restTemplate.exchange(url.toString(), HttpMethod.GET, httpEntity, type);

PageImpl 不包含默认构造函数,因此为了避免该问题,我创建了一个如下所示的类来传递给 ParameterizedTypeReference:

PageImpl contains no default constructor so to avoid that problem I created a class like the following one to pass to the ParameterizedTypeReference:

@JsonIgnoreProperties(ignoreUnknown = true) @Getter @Setter
public class RestResponsePage<T> extends PageImpl<T> implements Serializable {

    private static final long serialVersionUID = 3844794233375694591L;

    @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
    public RestResponsePage(@JsonProperty("content") List<T> content,
                        @JsonProperty("number") int page,
                        @JsonProperty("size") int size,
                        @JsonProperty("totalElements") long totalElements) {
        super(content, new PageRequest(page, size), totalElements);
    }

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

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

    public RestResponsePage() {
        super(new ArrayList<T>());
    }
}

问题是我仍然收到以下错误:

The problem is that I still get the following error:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.springframework.data.domain.Pageable: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: java.io.PushbackInputStream@75564689; line: 38, column: 16] (through reference chain: com.shaunyl.util.ResponsePageImpl["pageable"])

为什么它一直说我正在传递一个抽象类?ResponsePageImpl 是一个类,而不是一个抽象类.

Why it keeps saying that I am passing an abstract class? ResponsePageImpl is a class not an abstract class.

谢谢

推荐答案

package com.td.support;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.ArrayList;
import java.util.List;

public class RestResponsePage<T> extends PageImpl<T> {
    @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
    public RestResponsePage(@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 RestResponsePage(List<T> content, Pageable pageable, long total) {
        super(content, pageable, total);
    }

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

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

spring boot 2.0 以上可能没问题..

spring boot 2.0 may be ok above..

这篇关于使用 Pageable 字段测试端点时出现 JsonMappingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆