Jackson deserialize issue

37 views
Skip to first unread message

Paulomi Mukherjee

unread,
May 23, 2019, 3:18:22 PM5/23/19
to jackson-user
Hello,

I have a class Service

public class Service  {
    @NotNull
    @ApiModelProperty("")
    @XmlTransient
    @AttributeInfo(
            modifiable = "true"
    )
    public String name;

    @Max(10)
    @ApiModelProperty("")
    @XmlTransient
    @AttributeInfo(
            modifiable = "true"
    )
    public Integer serviceID;

    @JsonProperty("resource")
    @Relationship(
            type = "ASSOCIATES",
            direction = "OUTGOING"
    )
    protected Set<Resource> resource = new HashSet<>();

:
:
}
And class Resource

public class Resource  {

@Transient
@JsonIgnore
@XmlTransient
public String ref;
@GraphQLQuery(name = "ref")
@JsonIgnore
public String getRef() {
return ref;
}

@JsonProperty("@ref")
public void setRef(String ref) {
this.ref = ref;
}
    @ApiModelProperty("")
    @XmlTransient
    @AttributeInfo(
            modifiable = "true"
    )
    public String name;

    @ApiModelProperty("")
    @XmlTransient
    @AttributeInfo(
            modifiable = "true"
    )
    public String state;

    @JsonProperty("service")
    @Relationship(
            type = "ASSOCIATES",
            direction = "INCOMING"
    )
    protected Service service;
:
}

BAD CASE
When I create(POST) service object on http://localhost:8080/api/service with payload

{
    "name": "ServiceHOME",
    "serviceID": "1",
    "resource": [
        {
            "@ref": "/api/device/d581e0b8-eff0-4cff-ac45-6edb9292111e"
        },
        {
            "@ref": "/api/device/17707856-e769-49b9-a2ae-97caf74a03a2"
        }
    ]
}

The object created of service has only one resource object (with "@ref": "/api/device/d581e0b8-eff0-4cff-ac45-6edb9292111e") created. The second resource is missing completely.

GOOD CASE
Whereas, when I create(POST) service object on http://localhost:8080/api/service with payload

{
    "name": "ServiceHOME",
    "serviceID": "1",
    "resource": [
        {
            "@ref": "/api/device/d581e0b8-eff0-4cff-ac45-6edb9292111e"
        },
        {
            "@ref": "/api/device/17707856-e769-49b9-a2ae-97caf74a03a2",
    "name":"resourceName"
        }
    ]
}
The object created of service has two resource object (with "ref=/api/device/d581e0b8-eff0-4cff-ac45-6edb9292111e" and "ref=/api/device/17707856-e769-49b9-a2ae-97caf74a03a2 + name=resourceName" which is expected.

Even the below payload deserializes as expected (with three resource object inside service object):

{
    "name": "ServiceHOME",
    "serviceID": "1",
    "resource": [
        {
            "@ref": "/api/device/d581e0b8-eff0-4cff-ac45-6edb9292111e"
        },
        {
            "@ref": "/api/device/17707856-e769-49b9-a2ae-97caf74a03a2",
    "name":"resourceName"
        },
{
     "name":"resourceName"
        }
    ]
}

I tried debugging the deserializeFromObject method in BeanDeserializer in both good and bad cases and was able to see that bean is formed for two resource object. But then I got lost on why in the bad case the 2nd resource object was present in service object after deserialization. Hitting the walls on trying to understand why did the bad case did not deserialize as expected. Any ideas on what is going wrong?
Am using fasterxml 2.9.8 for jackson data binding and Spring boot version is 2.1.3.

Paulomi Mukherjee

unread,
May 24, 2019, 2:52:19 AM5/24/19
to jackson-user
Kindly ignore the query.
Mistake from my side.
Realised that my equals and hashcode method of resource class did not have ref. So, the new resource object was not getting added to the collection.
Reply all
Reply to author
Forward
0 new messages