0
votes

I have an entity like that:

@Entity
public class Content {
  @EmbeddedId
  private IdVersionPK key;

  @Column
  private String type;

  @ManyToOne
  private Contact contact;

  @ManyToOne
  private Menu menu;
}

I have this simple RepresentationModelAssembler:

@Component
public class ContentModelAssembler implements RepresentationModelAssembler<Content, EntityModel<Content>> {
  @Override
  public EntityModel<Content> toModel(final Content entity) {
    return new EntityModel<>(entity);
  }
}

When I define an endpoint like that:

@GetMapping()
public ResponseEntity<PagedModel<EntityModel<Content>>> getContent() {
  return ResponseEntity
      .ok()
      .body(pagedAssembler.toModel(repository.findAll(), contentAssembler));
}

And call the endpoint I get the following output:

ACTUAL

{
  "_embedded" : {
    "contents" : [
      {
        "type" : "NEWS",
        "contact": null,
      }
    ]
  }
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/contents"
    }
  },
  "page" : {
    "size" : 1,
    "totalElements" : 10,
    "totalPages" : 1,
    "number" : 0
  }
}

But what i would expect to get is:

EXPECTED

{
  "_embedded" : {
    "contents" : [
      {
        "key" : {
          "id" : 1,
          "version" : "1.0"
        },
        "type" : "NEWS",
        "contact": null,
        "menu" : {
          ...
        }
      }
    ]
  }
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/contents"
    }
  },
  "page" : {
    "size" : 1,
    "totalElements" : 10,
    "totalPages" : 1,
    "number" : 0
  }
}

So the difference is, that two of the three nested objects are not getting serialized. I would like to know why the contact field, which is null, is getting serialized and the fields key and menu, which are not null, are not getting serialized.

I need nested objects to get serialized too.