0

Here is the class

package com.bablo.domain;

import com.google.gson.annotation.SerializedName;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;


@Getter @Setter
@ToString
public class PC {

   @SerializeName("name")
   private String name;

   @SerializeName("model")
    private String model;

   @SerializeName("processor")
    private String processor;
}

In the above class attribute for model can have an empty string in the json, how to give it default value at the time of serialization and deserialization.

That is in the requestbody json the value for the model key can have empty string as a value, how to read it and put instead default values in the place of empty string in the class attribute.

Should I disable lombok and add custom getter and setter methods in my class to override the default values?

Or is there any annotation available in Gson to achieve this?

Or lombok has some workaround to achieve this?

John Doe
  • 2,752
  • 5
  • 40
  • 58
  • Take a look on [Setting Default value to a variable when deserializing using gson](https://stackoverflow.com/questions/30216317/setting-default-value-to-a-variable-when-deserializing-using-gson) – Michał Ziober Apr 02 '19 at 09:36

1 Answers1

1

Probably, you need to use lombok Builder annotation here. Something like this:

@Getter @Setter
@ToString
class PC {

    @SerializedName("name")
    @Builder.Default
    private String name = "name1";

    @SerializedName("model")
    @Builder.Default
    private String model = "model1";

    @SerializedName("processor")
    @Builder.Default
    private String processor = "processor1";
}