com.fasterxml.jackson.databind.JsonMappingException:无法反序列化org.springframework.data.domain.Instance的序列,超出了START_ARRAY令牌 [英] com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.springframework.data.domain.Sort out of START_ARRAY token

查看:185
本文介绍了com.fasterxml.jackson.databind.JsonMappingException:无法反序列化org.springframework.data.domain.Instance的序列,超出了START_ARRAY令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的objectMapper正在按如下方式获取类型Page<User>的值:

My objectMapper is fetching the value of type Page<User> as follows:

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

PageImplBean扩展了PageImpl类,如下所示:

The PageImplBean extends PageImpl class as follows:

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());
}
}

我的存储库类如下:

@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);
}    

我的服务类别如下:

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>对象由一个可分页的实例组成,该实例的对象Sort由值[addressId:ASC]组成.

My Page<User> object from the webservice consists of a pageable instance with object Sort which consists of value [addressId: ASC].

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

While deserializing the object to userList with objectMapper as shown above I'm encountering the mentioned error.

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

任何帮助,不胜感激.谢谢.

Any help much appreciated. Thank you.

推荐答案

今天早上我遇到了这个确切的问题.

I ran into this exact issue this morning.

CustomSortDeserializer

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)方法:

Then added the following the the PageImplBean's public void setSort(Sort sort) method:

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

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

Here is the JSON I received to deserialize (from 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
  }
]

如果您查看

If you look at Sort, you'll see that it implements Iteratable for Order. And if you examine Order defined in the same class, you'll see the fields in my example JSON. So when Sort is serialized, it is serialized as a list of Order, which is why deserializing into Sort needs a custom deserializer implementation, and also why deserializing without a custom implementation fails (because it's trying to deserialize an array into a single object).

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

During deserialization, when a setter annotated with @JsonDeserializer is encountered as each property is deserialized, Jackson uses the supplied deserializer to deserialize that specific property.

这篇关于com.fasterxml.jackson.databind.JsonMappingException:无法反序列化org.springframework.data.domain.Instance的序列,超出了START_ARRAY令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆