java - 使用 Pageable 字段测试端点时出现 JsonMappingException

标签 java rest spring-boot jackson

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

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

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

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:

@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>());
    }
}

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

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 是一个类而不是抽象类。

谢谢

最佳答案

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上面可能没问题..

关于java - 使用 Pageable 字段测试端点时出现 JsonMappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50826061/

相关文章:

java - Swing 和 iText 字体渲染之间的差异

objective-c - 在适用于 iOS 的 Restful 协议(protocol)中推送通知

java - 使用 rest 客户端将 excel 文件上传到 rest 服务会导致 400 Bad request

java - 在 Spring Boot 中从生成 PDF 内容类型内容的外部 Rest API 中提取响应字节的正确方法是什么?

javascript - backbone.js ajax 请求的全局错误处理程序

java - 集成 openid connect 进行 SPA 应用的最佳方式

java - 当类不是 Bean 时,如何在字段变量上使用 @Autowired?

java - 无法弄清楚为什么我得到空值

java - (应用程序崩溃)返回空指针异常...当从 Serilizable 类调用公共(public)类的方法时

java - 按下按钮时绘制线条 Swing