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

时间:2016-11-01 10:34:48

标签: java spring spring-data-rest

我正在为我的应用程序使用spring数据休息。当我执行电话后。我收到错误

输入有效内容语法: -

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

2 个答案:

答案 0 :(得分:0)

question (and answer)。显然,Spring没有针对Multiset的反序列化器,所以你必须创建一个,或者更改一些更标准的类型(HashSet可能吗?)

答案 1 :(得分:0)

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

我使用的依赖jar暴露的类之一是:

List < SomeClass > listOfItems;

我这样修改了我的objectMapper:

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