我需要调用一个需要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