java - 在 Spring 数据休息中找不到非具体 Collection 类型的反序列化器

标签 java spring spring-data-rest

我正在为我的应用程序使用 Spring Data Rest。当我执行后调用时。我收到错误

输入负载语法:-

{
  "id": 0,
  "name": "string",
  "sortedWeightedSources": {
    "empty": true
  },
  "status": "ACTIVE",
  "weightedSources": [
    {
      "sources": {
        "accountNumber": "string",
        "active": true,
        "configuration": "string",
        "id": 0,
        "name": "string"
      },
      "rate": 0,
      "id": 0,
      "status": "ACTIVE",
      "weight": 0
    }
  ]
}

请求:-

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/hal+json' -d '{ \ 
   "name": "string"   \ 
 }' 'http://localhost:8082/model1'

错误:-

{
  "cause": {
    "cause": {
      "cause": null,
      "message": "Can not find a deserializer for non-concrete Collection type [collection type; class com.google.common.collect.Multiset, contains [simple type, class com.test.model.weightedSources]]"
    },
    "message": "Can not find a deserializer for non-concrete Collection type [collection type; class com.google.common.collect.Multiset, contains [simple type, class com.test.model.weightedSources]]\n at [Source: org.apache.catalina.connector.CoyoteInputStream@628d6cf8; line: 1, column: 1]"
  },
  "message": "Could not read document: Can not find a deserializer for non-concrete Collection type [collection type; class com.google.common.collect.Multiset, contains [simple type, class com.test.model.weightedSources]]\n at [Source: org.apache.catalina.connector.CoyoteInputStream@628d6cf8; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not find a deserializer for non-concrete Collection type [collection type; class com.google.common.collect.Multiset, contains [simple type, class com.test.model.weightedSources]]\n at [Source: org.apache.catalina.connector.CoyoteInputStream@628d6cf8; line: 1, column: 1]"
}

模型1:-

public class Model1 implements Serializable {

    int id;
    private String name;
    private Status status;
    private Set<WeightedSource> weightedSources;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Enumerated(EnumType.STRING)
    @Column(name = "status")
    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    @OneToMany(mappedBy = "model1", fetch = FetchType.EAGER)
    public Set<WeightedSource> getWeightedSources() {
        return WeightedSources;
    }

    public void setWeightedSources(Set<WeightedDemandSource> weightedSources) {
        this.weightedSources = weightedSources;
    }

    @Transient
    public Multiset<WeightedSource> getSortedWeightedSources() {
        Multiset<WeightedSource> set = TreeMultiset.create();
        if (weightedSources != null) {
            for (WeightedSource weightedSource : weightedSources) {
                set.add(weightedSource);
            }
        }
        return set;
    }

    public void addWeightedSource(WeightedSource weightedSource) {
        weightedSource.setChain(this);
        if (weightedSources == null) {
            weightedSources = Sets.newHashSet();
        }
        weightedSources.add(weightedSource);
    }


}

模型2:-

public class WeightedSource implements Serializable, Comparable<WeightedSource> {
Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private int id;
    @ManyToOne(targetEntity = Source.class)
    @JoinColumn(name = "source_id", nullable = false)
    private Source source;

    @JsonIgnore
    @ManyToOne(targetEntity = Model1.class)
    @JoinColumn(name = "model1_id", nullable = false)
    private Model1 model1;
    private int weight;
    private Status status;
    private int hitRate;
}

最佳答案

我在单元测试中也遇到了这个问题。

我使用的依赖 jar 公开的类之一是:

List < SomeClass > listOfItems;

我像这样修改了我的 objectMapper :

objectMapper.registerModules(new GuavaModule());
objectMapper.registerSubtypes(ClassToSerialize.class);

关于java - 在 Spring 数据休息中找不到非具体 Collection 类型的反序列化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40358342/

相关文章:

java - JSON 路径结果中的通用列表类型

java - 在外部缓存中缓存分离的 Hibernate 实体

java - 您可以将@Autowired 与静态字段一起使用吗?

java - 在 Spring Security 中,UserDetails 将密码存储为 String,但我将其存储为 byte[]

java - 为什么 Spring Boot 对实体的 "404 Not Found"处理方式不同?

JSON 不返回 Spring Data REST 中的列

java - 如何自定义 Spring Data REST 以对存储库资源使用多段路径?

java - log4j:错误无法解析文件

java - 如何强制 socket.close 释放写 block

java - 如何禁用/隐藏 JxBrowser 许可证信息