feign page exception
spring cloud feign page 分页异常,没有无参构造方法,反序列化失败
方法一:定义自己的 Page 类,在服务端自己转化
影响较大,需要修改服务端代码
public class PageFeign<T> implements Serializable {
private int pageNo;
private int pageSize;
private int total;
private List<T> Content;
public PageFeign(){}
public PageFeign(Page<T> page){
//Page<T> convert to PageFeign
}
//getter and setter
}
//修改服务端代码 将 Page<T> 转换成 PageFeign<T>
@GetMapping(value = "/page")
public Page<Member> page(@RequestParam(defaultValue = "0") int pageNo,
@RequestParam(defaultValue = "10") int pageSize,
@RequestParam Map<String, Object> queryMap){
Page<Member> page = memberService.page(pageNo, pageSize, member, queryMap);
return new PageFeign(page);
}
方法二:定义自己的 Page 类,在消费端接收即可
自定义 PageFeign<T>
类,在消费端接收分页对象 Page<T>
public class PageFeign<T> implements Serializable {
private static final long serialVersionUID = 1L;
private List<T> content = new ArrayList<>();
private boolean last;
private boolean first;
private boolean empty;
private int totalPages;
private int totalElements;
private int numberOfElements;
private int size;
private int number;
//此处本来是 Pageable, Sort对象,自己定义对象也可以,但用Map也能满足
private Map<String, Object> pageable = new HashMap<>();
private Map<String, Boolean> sort = new HashMap<>();
// getter and setter
}
消费端使用
//import com.lxyng.speb.util.PageFeign;
@FeignClient(value = "memberService")
public interface MemberFeignClient {
@GetMapping(value = "/v1/member/page", consumes = "application/json")
PageFeign<Member> page(@RequestParam("pageNo") int pageNo,
@RequestParam("pageSize") int pageSize,
@RequestParam("queryMap")Map<String, Object> queryMap);
}
服务端代码
//import org.springframework.data.domain.Page;
@GetMapping(value = "/page")
public Page<Member> page(@RequestParam(defaultValue = "0") int pageNo,
@RequestParam(defaultValue = "10") int pageSize,
@RequestParam Map<String, Object> queryMap){
return memberService.page(pageNo, pageSize, member, queryMap);
}
吐槽下 org.springframework.data.domain.Page 反人类的页码从 0 开始