1

I am working on a model that involves the data type boolean, so the issue with Jackson is that if I put in isValid it will auto convert to valid in the schema. However this model schema is in need to store into MongoDB, and I want isValid to be the field, not valid.

So I searched around and I read that @Field("isValid") should solve the issue, however it is not and it is causing @JsonProperty("isValid") not working as well.

What happened here is when I put only @JsonProperty("isValid"), it can be serialized and deserialized as isValid perfectly, however when I try to insert into MongoDB, the field change into valid instead of isValid.

Here are my code for the POJO:

@Data
@ToString
public class Response {
    private String duration;

    private String time;

    @JsonProperty("isValid")
    @Field("isValid")
    private boolean isValid;
}

This is what I want inside the MongoDB:

{
   "duration": "313610236",
   "isValid": false,
   "time": "1658304521794"
}

However now I have:

{
   "duration": "313610236",
   "valid": false,
   "time": "1658304521794"
}

How can I achieve both function where I get to deserialize and serialize as isValid but also saved as isValid in terms of field into the MongoDB?

Thank you!

Sivvie Lim
  • 784
  • 2
  • 14
  • 43

1 Answers1

-1

This might work for you:

@JsonIgnoreProperties({"valid"}) // Prevents serialization
@Data
@ToString
public class Response {
    private String duration;

    private String time;

    @JsonProperty("isValid")
    private boolean isValid;
}

Found a link that might be helpful for you: Jackson renames primitive boolean field by removing 'is'

CurssedHammer
  • 89
  • 1
  • 10