Java的端点_java – 使用Pageable字段测试端点时的JsonMappingException

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

@GetMapping

public Page listProducts(Pageable pageable) {

return productService.findProducts(pageable);

}

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

MultiValueMap parameters = new LinkedMultiValueMap<>();

parameters.add("page", String.valueOf(0));

URI url = defaultURI(port, "/products", parameters);

ParameterizedTypeReference> type = new ParameterizedTypeReference>() {};

ResponseEntity> response = restTemplate.exchange(url.toString(), HttpMethod.GET, httpEntity, type);

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

@JsonIgnoreProperties(ignoreUnknown = true) @Getter @Setter

public class RestResponsePage extends PageImpl implements Serializable {

private static final long serialVersionUID = 3844794233375694591L;

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)

public RestResponsePage(@JsonProperty("content") List content,

@JsonProperty("number") int page,

@JsonProperty("size") int size,

@JsonProperty("totalElements") long totalElements) {

super(content, new PageRequest(page, size), totalElements);

}

public RestResponsePage(List content, Pageable pageable, long totalElements) {

super(content, pageable, totalElements);

}

public RestResponsePage(List content) {

super(content);

}

public RestResponsePage() {

super(new ArrayList());

}

}

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

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 extends PageImpl {

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)

public RestResponsePage(@JsonProperty("content") List 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 content, Pageable pageable, long total) {

super(content, pageable, total);

}

public RestResponsePage(List content) {

super(content);

}

public RestResponsePage() {

super(new ArrayList<>());

}

}

弹簧靴2.0可能上面没问题..

标签:java,spring-boot,rest,jackson

来源: https://codeday.me/bug/20190910/1800085.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值