问题概述:使用springcloud feign 微服务间调用时,返回page对象时,出现Page(缺少默认构造方法)实例化错误,

github上该问题好像已经解决【https://github.com/spring-cloud/spring-cloud-netflix/issues/556】



A项目中方法
@PostMapping("/users")
@Timed
public ResponseEntity<Page<UserDTO>> abilities(@RequestBody QueryDTO query, @ApiParam Pageable pageable) {
    log.debug("REST request to get a page of Users");

    Page<UserDTO> page = userService.findAll(query, pageable);

    return ResponseEntity.ok(page);
}

B项目中调用A项目方法(pageable => page & size,自定义PageFeign.class用来接收返回的Page对象)

@RequestMapping(value = "/api/users", method = RequestMethod.POST)

PageFeign<UserDTO> users(@RequestBody QueryDTO query, @RequestParam("page") int page, @RequestParam("size") int size);

public class PageFeign<T> implements Iterable<T>, Serializable {
    
private static final long serialVersionUID = 1L;

private List<T> content = new ArrayList<>();
private boolean last;
private int totalPages;
private int totalElements;
private int numberOfElements;
private int size;
private int number;
private Pageable pageable;

public PageFeign() {
}

public List<T> getContent() {
    return content;
}

public void setContent(List<T> content) {
    this.content = content;
}

public boolean isLast() {
    return last;
}

public void setLast(boolean last) {
    this.last = last;
}

public int getTotalPages() {
    return totalPages;
}

public void setTotalPages(int totalPages) {
    this.totalPages = totalPages;
}

public int getTotalElements() {
    return totalElements;
}

public void setTotalElements(int totalElements) {
    this.totalElements = totalElements;
}

public int getNumberOfElements() {
    return numberOfElements;
}

public void setNumberOfElements(int numberOfElements) {
    this.numberOfElements = numberOfElements;
}

public int getSize() {
    return size;
}

public void setSize(int size) {
    this.size = size;
}

public int getNumber() {
    return number;
}

public void setNumber(int number) {
    this.number = number;
}

public Pageable getPageable() {
    return new PageRequest(number,size);
}

}

    /** PageUtil.class 用来转换对象 **/ PageFeign -> Page

public static <T> Page<T> localPageToSpringPage(PageFeign<T> page) {
    return new PageImpl(page.getContent(), page.getPageable(), page.getTotalElements());
}



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐