Search code examples
spring-bootmapstruct

how to hide key in payload if value of that key is null using mapstruct in springboot


example dto generated:

{
  "id": 1,
  "name": "test",
  "attributes": null
}

expecting response:

{
  "id": 1,
  "name": "test"
}

here I have to ignore attributes while returning. I cannot use @JsonInclude(JsonInclude.Include.NON_NULL) in dto because it has to be auto generated for other reasons and I cannot give @NotNull in model because it can be null for some cases. Can someone please help me with this?


Solution

  • You can easily add @JsonInclude(JsonInclude.Include.NON_NULL) to your Entity .

    .@JsonInclude(JsonInclude.Include.NON_NULL) public class RealPerson implements Serializable {...