1

I have an object myObject with many values set to null, first set to 502296999 and the second is empty (""). In response I have:

"Phones": {
            "First": "502296999"
}

while I would like to receive this:

 "Phones": {
                "First": "502296999",
                "Second": ""

}

I am building the response this way:

return Response.ok(myObject).build();

Is there an easy way to also put the fields with value "" into response? Or to force Response.ok(myObject).build() to always add those two fields no matter what is inside?

Michu93
  • 5,058
  • 7
  • 47
  • 80
  • How is `myObject` populated? For example, consturctor to set default values, or setters which check for null and convert to "". But why do this? It's more code to maintain and makes the consuming client do more work which would have to check for a non-empty value. Also, consider a `List phones` (easily extensible and consumable), compared to `First`, `Second`, etc. – Andrew S May 06 '19 at 15:39
  • `myObject` is created by constructor with values taken from body of REST-PUT method. I have noticed that the `build()` takes all not null and not empty parameters and I would like to change it to doesn't take just nulls. – Michu93 May 06 '19 at 15:46
  • 1
    It depends on the code of the class of myObject, on which JSON marshaller you're using, and on how you configured it. And we know nothing of all of that. – JB Nizet May 06 '19 at 21:34
  • @JBNizet it's just a standard DAO class with `private String first;` and `private String second;` with getters, setters, overriden `hashCode()`, `equals()` and `toString()` – Michu93 May 07 '19 at 07:36

1 Answers1

1

My bad, other function in mediator was checking if value is empty or null and didn't displaying it. By default Response.ok(object).build() return also the empty values:

"Phones": {
            "First": "502296999",
            "Second": ""
}

I have found that it can be configured by annotation @JsonInclude. Example here: How to tell Jackson to ignore a field during serialization if its value is null?

Michu93
  • 5,058
  • 7
  • 47
  • 80