简体   繁体   English

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

My objectMapper is fetching the value of type Page<User> as follows: 我的objectMapper获取类型为Page<User>的值,如下所示:

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

The PageImplBean extends PageImpl class as follows: PageImplBean扩展了PageImpl类,如下所示:

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

My repository class looks as follows: 我的存储库类如下所示:

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

My service class is as follows: 我的服务类如下:

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

My Page<User> object from the webservice consists of a pageable instance with object Sort which consists of value [addressId: ASC]. 来自webservice的My Page<User>对象由一个带有对象Sort的可分页实例组成,该对象包含值[addressId:ASC]。

While deserializing the object to userList with objectMapper as shown above I'm encountering the mentioned error. 使用objectMapper将对象反序列化为userList,如上所示,我遇到了上述错误。

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

Then added the following the the PageImplBean's public void setSort(Sort sort) method: 然后在PageImplBean的public void setSort(Sort sort)方法中添加以下内容:

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

Edit: 编辑:

Here is the JSON I received to deserialize (from spring-data-commons - 1.9.2.RELEASE): 这是我收到的用于反序列化的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
  }
]

If you look at Sort , you'll see that it implements Iteratable for Order. 如果你看一下Sort ,你会发现它为Order实现了Iteratable。 And if you examine Order defined in the same class, you'll see the fields in my example JSON. 如果您检查在同一个类中定义的Order,您将看到我的示例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). 因此,当Sort被序列化时,它被序列化为Order列表,这就是为什么反序列化为Sort需要自定义反序列化器实现的原因,以及为什么没有自定义实现的反序列化失败(因为它试图将数组反序列化为单个对象)。

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. 在反序列化期间,当遇到使用@JsonDeserializer注释的setter时,每个属性都被反序列化,Jackson使用提供的反序列化器来反序列化该特定属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.fasterxml.jackson.databind.JsonMappingException:无法从START_ARRAY标记中反序列化TestAcceptanceCriteria的实例 - com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of TestAcceptanceCriteria out of START_ARRAY token com.fasterxml.jackson.databind.JsonMappingException:无法反序列化START_ARRAY令牌 - com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize out of START_ARRAY token 错误:com.fasterxml.jackson.databind.JsonMappingException:无法从START_ARRAY令牌中反序列化Entities.Student的实例 - Error: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of Entities.Student out of START_ARRAY token com.fasterxml.jackson.databind.JsonMappingException:无法从START_OBJECT令牌中反序列化java.util.ArrayList实例 - com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从START_ARRAY令牌中反序列化对象的实例 - com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token com.fasterxml.jackson.databind.JsonMappingException:无法反序列化VALUE_NUMBER_INT令牌中的java.util.ArrayList实例 - com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_NUMBER_INT token com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化 Object 的实例超出 START_ARRAY 令牌 - com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of Object out of START_ARRAY token com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_ARRAY 令牌中反序列化对象实例 - JAVA - com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token - JAVA 引起:org.codehaus.jackson.map.JsonMappingException:无法从START_ARRAY令牌中反序列化com.model.user的实例 - Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.model.user out of START_ARRAY token org.codehaus.jackson.map.JsonMappingException:无法从START_ARRAY令牌中反序列化java.lang.Long的实例 - org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.Long out of START_ARRAY token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM