Serializing Object does not serialize last element in Set.

17 views
Skip to first unread message

Kiren Pillay

unread,
Jul 16, 2019, 9:17:52 PM7/16/19
to jackson-user
Hi

I'm having an issue where the last element in my Object's list is not getting serialized. Debugging shows that the required values for Categories are present.

Submitting a number of times, the order of this list can change, and the pattern is that the last element is never serialized. Any ideas?
  V2.9.9.
spring-boot:  2.1.6.RELEASE


Each object in array is a Vendor, and the object in categories is Category.

{
    "4": [
        {
            "id": 76,
            "active": 0,
            "address": "",
            "contactCell": "",
            "contactEmail": "",
            "contactFirstName": "test25",
            "contactSurname": "tester25",
            "description": "",
            "firebaseUserId": "534k5d3gfhj554",
            "latitude": "",
            "logoImage": "",
            "longitude": "",
            "name": "kires2hhhgghh",
            "categories": [
                {
                    "id": 5,
                    "name": "Clothing"
                },
                {
                    "id": 4,
                    "name": "Cosmetics"
                }
            ]
        },
        {
            "id": 72,
            "active": 1,
            "address": "",
            "contactCell": "",
            "contactEmail": "",
            "contactFirstName": "test25",
            "contactSurname": "tester25",
            "description": "",
            "firebaseUserId": "534k5d3gfhj554",
            "latitude": "",
            "logoImage": "",
            "longitude": "",
            "name": "kirenbss2ghhh",
            "categories": [
                {
                    "id": 1,
                    "name": "Pet Related"
                },
                4     <---Expecting a Category Object like the one in Object id 76.
            ]
        }
    ]
}

Alternate Response:

{
    "4": [
        {
            "id": 72,
            "active": 1,
            "address": "",
            "contactCell": "",
            "contactEmail": "",
            "contactFirstName": "test25",
            "contactSurname": "tester25",
            "description": "",
            "firebaseUserId": "534k5d3gfhj554",
            "latitude": "",
            "logoImage": "",
            "longitude": "",
            "name": "kirenbss2ghhh",
            "categories": [
                {
                    "id": 4,
                    "name": "Cosmetics"
                },
                {
                    "id": 1,
                    "name": "Pet Related"
                }
            ]
        },
        {
            "id": 76,
            "active": 0,
            "address": "",
            "contactCell": "",
            "contactEmail": "",
            "contactFirstName": "test25",
            "contactSurname": "tester25",
            "description": "",
            "firebaseUserId": "534k5d3gfhj554",
            "latitude": "",
            "logoImage": "",
            "longitude": "",
            "name": "kires2hhhgghh",
            "categories": [
                {
                    "id": 5,
                    "name": "Clothing"
                },
                4
            ]
        }
    ]
}

Code:

Controller

    @GetMapping("categories/filter")
    public ResponseEntity<Map<Object, Object>> getVendorByCategory(@RequestBody Set<Category> categories) {
        Map<Object, Object> vendor = service.findAllCategories(categories);
            return new ResponseEntity<Map<Object,Object>>(vendor, HttpStatus.OK);
    }

--Service
public Map<Object, Object> findVendorsForEachCategory(Set<Category> categories) {
        Iterable<Long> ids=categories.stream().map(c->c.getId()).collect(Collectors.toList());
        HashSet<Category> matches = new HashSet<Category>( categoryRepo.findAllById(ids));
       
        Map<Object, Object> vendors=matches.stream().collect(Collectors.toMap(c->c.getId(),c->c.getVendors()));
        return vendors;
    }

--Vendor object (trimmed)
@Entity
@Table(name="vendor")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Vendor implements Serializable {
    private static final long serialVersionUID = 1L;
   
      @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Long id;

 
    //bi-directional many-to-many association to Category
       
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
                name="vendorCategory"
                , joinColumns={
                    @JoinColumn(name="vendorId", referencedColumnName="id")
                    }
                , inverseJoinColumns={
                        @JoinColumn(name="categoryId", referencedColumnName="id")
                }
                )
    private Set<Category> categories;

 
    public Set<Category> getCategories() {
        return this.categories;
    }

    public void setCategories(Set<Category> categories) {
        this.categories = categories;
    }
      public Long getId() {
        return id;
    }

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

}

-------------Category
Entity
@Table(name = "category")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")

public class Category implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    // bi-directional many-to-many association to Vendor
   
    @ManyToMany(mappedBy = "categories")
    @JsonBackReference
    private Set<Vendor> vendors;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    public Long getId() {
        return id;
    }

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

    public Category() {
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
   
    public Set<Vendor> getVendors() {
        return this.vendors;
    }

    public void setVendors(Set<Vendor> vendors) {
        this.vendors = vendors;
    }

}

Tatu Saloranta

unread,
Jul 18, 2019, 1:23:02 AM7/18/19
to jackson-user
On Tue, Jul 16, 2019 at 6:17 PM Kiren Pillay <kirenp...@gmail.com> wrote:
>
> Hi
>
> I'm having an issue where the last element in my Object's list is not getting serialized. Debugging shows that the required values for Categories are present.
>
> Submitting a number of times, the order of this list can change, and the pattern is that the last element is never serialized. Any ideas?

You are using `@JsonIdentityInfo`, in which only the first instance of
given Object is serialized fully, but then later (in serialization
order) are replaced by Object Id.
This is done sometimes to reduce output size, but more often to allow
handling of cyclic graphs.

If you do not want this to happen, you should not use the annotation.

I hope this helps,

-+ Tatu +-
> --
> You received this message because you are subscribed to the Google Groups "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
> To post to this group, send email to jackso...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/3903ef41-c94c-4ca9-8cc4-3344ae691308%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Kiren Pillay

unread,
Jul 22, 2019, 5:35:03 AM7/22/19
to jackso...@googlegroups.com
Hi Tatu

Thank you, removing the annotation fixed the problem.

Regards
Kiren

Reply all
Reply to author
Forward
0 new messages