3

I am using spring data rest for my application. When I execute post call. I am getting error

Input payload Syntax:-

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

Request:-

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

Error:-

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

Model1:-

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


}

Model2:-

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;
}
Galet
  • 5,853
  • 21
  • 82
  • 148

2 Answers2

8

I was also facing this issue in my unit tests.

One of the classes exposed by dependent jar I was using was having:

List < SomeClass > listOfItems;

I modified my objectMapper like this :

objectMapper.registerModules(new GuavaModule());
objectMapper.registerSubtypes(ClassToSerialize.class);
01000001
  • 833
  • 3
  • 8
  • 21
1

There is this question (and answer). Apparently, Spring doesn't have a deserializer for Multiset, so you have to either create one, or change the type for something more standard (HashSet maybe?)

Community
  • 1
  • 1
Igor Deruga
  • 1,504
  • 1
  • 10
  • 18