在我最近的项目中,我需要发送分页列表。我在较早的项目中做了分页。然后我没有遇到任何错误。但是现在我无法在最近的项目中进行分页。我不知道怎么了。我正在逐步提供我的代码。
这是我的控制器类,它返回一个自定义页面类
@GetMapping(value = "")
public CustomerResponsePage getCustomerList(@RequestParam Long agentId,
@RequestParam(value = "page", defaultValue = "0") Integer page) {
if (agentId == null)
throw new NullValueException("Null value received for agentId");
return customerService.getCustomersOfAgent(agentId, page);
}
我的服务方式是
@Override
public CustomerResponsePage getCustomersOfAgent(Long agentId, Integer page) {
User agent = userService.read(agentId);
if (agent == null)
throw new EntityNotFoundException("Agent with this id is not found");
if (agent.getReferenceNo() == null)
throw new EntityNotFoundException("No reference found for this agent");
Page<User> customers = userRepository.findByReferenceNo(agent.getReferenceNo(),
new PageRequest(page, PageAttr.PAGE_SIZE));
List<UserDTO> dtoList = new ArrayList<>();
for (User user : customers) {
dtoList.add(customerMapper.customerResponseMapper(user));
}
System.out.println("Page is " + customers.getContent());
System.out.println("List is " + dtoList);
return new CustomerResponsePage(dtoList, customers);
}
我的自定义页面类是
public class CustomerResponsePage implements Page {
private List<UserDTO> content;
private Pageable pageable;
private boolean last;
private Integer totalPages;
private Long totalElements;
private Integer size;
private Integer number;
private Integer numberOfElements;
private boolean first;
private Sort sort;
public CustomerResponsePage(List<UserDTO> content, Page page) {
this.content = content;
this.pageable = page.getPageable();
this.last = page.isLast();
this.totalPages = page.getTotalPages();
this.totalElements = page.getTotalElements();
this.size = page.getSize();
this.number = page.getNumber();
this.numberOfElements = page.getNumberOfElements();
this.first = page.isFirst();
this.sort = page.getSort();
System.out.println("content is " + content.toString());
System.out.println("Page value is " + page.getTotalElements());
System.out.println("Content is " + page.getContent());
}
public void setContent(List<UserDTO> content) {
this.content = content;
}
@Override
public Pageable getPageable() {
return pageable;
}
@Override
public int getTotalPages() {
return this.totalPages;
}
@Override
public long getTotalElements() {
return this.totalElements;
}
@Override
public Page map(Function function) {
return null;
}
@Override
public int getNumber() {
return this.number;
}
@Override
public int getSize() {
return this.size;
}
@Override
public int getNumberOfElements() {
return this.numberOfElements;
}
@Override
public List getContent() {
return this.content;
}
@Override
public boolean hasContent() {
return this.content.size() > 0;
}
@Override
public Sort getSort() {
return this.sort;
}
@Override
public boolean isFirst() {
return this.first;
}
@Override
public boolean isLast() {
return this.last;
}
@Override
public boolean hasNext() {
return !this.isLast();
}
@Override
public boolean hasPrevious() {
return !this.isFirst();
}
@Override
public Pageable nextPageable() {
return null;
}
@Override
public Pageable previousPageable() {
return null;
}
@Override
public Iterator iterator() {
return null;
}
}
在我的控制台中,出现了
之类的错误Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.sweetitech.capitalexpress.pageable.CustomerResponsePage["empty"])]
请注意,CustomerResponsePage
内的打印方法有效。
答案 0 :(得分:1)
堆栈跟踪引用CustomerResponsePage
中名为empty
的字段。问题可能与该字段的序列化或其getter方法的结果有关。
如果您不需要在JSON输出中使用该字段,则可以使用@JsonIgnore
来跳过它。
[编辑]
在您的代码段中,使用CustomerResponsePage
手动实例化了new CustomerResponsePage(dtoList, customers)
。通过手动实例化,Spring将无法像往常一样处理分页。
我将使用map
提供的Page
函数,而不是手动遍历User
对象来创建对象。然后,您将不需要CustomerResponsePage
作为对象的包装,而可以返回Page<UserDto>
。
Page<User> customers = userRepository.findByReferenceNo(agent.getReferenceNo(),
new PageRequest(page, PageAttr.PAGE_SIZE));
Page<UserDto> userDtos = customers.map(c -> customerMapper.customerResponseMapper(c));
return userDtos;