com.fasterxml.jackson.databind.JsonMappingException:无法从 START_ARRAY 令牌中反序列化 org.springframework.data.domain.Sort 的实例

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.springframework.data.domain.Sort out of START_ARRAY token

我的 objectMapper 正在获取类型 Page<User> 的值,如下所示:

userList = objectMapper.readValue(RestAdapter.get(url), new TypeReference<PageImplBean<User>>(){});

PageImplBean 扩展 PageImpl class 如下:

public class PageImplBean<T> extends PageImpl<T> {
private static final long serialVersionUID = 1L;
private int number;
private int size;
private int totalPages;
private int numberOfElements;
private long totalElements;
private boolean previousPage;
private boolean first;
private boolean nextPage;
private boolean last;
private List<T> content;
private Sort sort;

public PageImplBean() {
    super(new ArrayList<T>());
}

public int getNumber() {
    return number;
}

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

public int getSize() {
    return size;
}

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

public int getTotalPages() {
    return totalPages;
}

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

public int getNumberOfElements() {
    return numberOfElements;
}

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

public long getTotalElements() {
    return totalElements;
}

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

public boolean isPreviousPage() {
    return previousPage;
}

public void setPreviousPage(boolean previousPage) {
    this.previousPage = previousPage;
}



public boolean isNextPage() {
    return nextPage;
}

public void setNextPage(boolean nextPage) {
    this.nextPage = nextPage;
}


public boolean isFirst() {
    return first;
}

public void setFirst(boolean first) {
    this.first = first;
}

public boolean isLast() {
    return last;
}

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

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

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

public Sort getSort() {
    return sort;
}

public void setSort(Sort sort) {
    this.sort = sort;
}

public PageImpl<T> pageImpl() {
    return new PageImpl<T>(getContent(), new PageRequest(getNumber(),
            getSize(), getSort()), getTotalElements());
}
}

我的存储库 class 如下所示:

@EnableScan
@EnableScanCount
public abstract interface UserRepository extends PagingAndSortingRepository<User, String>
{
  public abstract Page<User> findByAddressId(String paramString, Pageable paramPageable);

  public abstract Page<User> findAll(Pageable paramPageable);
}    

我的服务class如下:

public Page<User> fetchUserList(String addressId,Integer pageNumber, Integer pageSize){
    Page<User> userPageList = null;

        PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, Direction.ASC, "addressId");
        userPageList = userRepository.findByAddressId(addressId, pageRequest);

    return userPageList;
}

来自 Web 服务的我的 Page<User> 对象包含一个带有对象排序的可分页实例,该实例包含值 [addressId: ASC]。

如上所示使用 objectMapper 将对象反序列化为 userList 时遇到上述错误。

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.springframework.data.domain.Sort out of START_ARRAY token

非常感谢任何帮助。谢谢。

今天早上我 运行 正讨论这个问题。

CustomSortDeserializer

import java.io.IOException;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

public class CustomSortDeserializer extends JsonDeserializer<Sort> {

@Override
public Sort deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    ArrayNode node = jp.getCodec().readTree(jp);
    Order[] orders = new Order[node.size()];
    int i = 0;
    for(JsonNode obj : node){
        orders[i] =  new Order(Direction.valueOf(obj.get("direction").asText()), obj.get("property").asText());
        i++;
    }
    Sort sort = new Sort(orders);
    return sort;
}

然后在 PageImplBean 的 public void setSort(Sort sort) 方法中添加以下内容:

@JsonDeserialize(using=CustomSortDeserializer.class)
public void setSort(Sort sort) {
    this.sort = sort;
}

编辑:

这是我收到的要反序列化的 JSON(来自 spring-data-commons - 1.9.2.RELEASE):

[
  {
    "direction": "ASC",
    "property": "amount",
    "ignoreCase": false,
    "nullHandling": "NATIVE",
    "ascending": true
  },
  {
    "direction": "ASC",
    "property": "effectiveDate",
    "ignoreCase": false,
    "nullHandling": "NATIVE",
    "ascending": true
  }
]

如果您查看 Sort,您会发现它实现了 Iteratable for Order。如果您检查在同一个 class 中定义的订单,您将看到我的示例 JSON 中的字段。所以当 Sort 被序列化时,它被序列化为 Order 的列表,这就是为什么反序列化为 Sort 需要自定义反序列化器实现,也是为什么没有自定义实现反序列化失败的原因(因为它试图将数组反序列化为单个对象)。

在反序列化期间,当每个 属性 被反序列化时遇到用 @JsonDeserializer 注释的 setter 时,Jackson 使用提供的反序列化器反序列化该特定的 属性.