1

I have an entity, defined using a data class:

@Entity
data class BlogPost(

    (...)

    val title: String,

    (...)

    val slug: String = title.toSlug(),

)

As per this answer, I expected that a JSON object without a slug field would deserialize to a BlogPost whose slug was created using my .toSlug() extension method – but, to my surprise, the deserialized .slug ends up being null even though I thought it wasn't nullable (it's a String, not a String?).

I've tried using @JsonInclude(Include.NON_NULL), but I suspect this is the wrong tactic, and it only seems to affect serialization; the missing JSON value is still deserialized to null rather than my default.

I'm on Spring Boot 2.4.3.

  • Could you give a complete runnable example? – xjcl Mar 24 '21 at 13:11
  • An example would help, but I'm guessing something like JPA or Jackson creating the object and is using reflection to bypass the default value. You can either check and set the correct value using the `copy` function on the data class when relevant or add a getter that checks for null and returns the default. – DCTID Mar 30 '21 at 02:57

0 Answers0