While trying to serialize the following Object, which has a LocalDate field, with Gson, it works without any problem without using the @Expose annotation.
@Data
@Entity(name = "user")
@NoArgsConstructor
public class User {
@NotEmpty
@NotNull
@Id
@Column(unique = true)
@Expose
private String userName;
@NotEmpty
@NotNull
private String password;
@Past @After1900
@Expose
private LocalDate birthday;
@Email
@Expose
private String email;
@EqualsAndHashCode.Exclude
@OneToMany(mappedBy = "user", fetch= FetchType.LAZY, cascade = CascadeType.ALL)
@Expose
private List<Address> addressList;
}
With the following code,
Gson gson = new Gson();
System.out.println(gson.toJson(user));
I get the following Json,
{
"userName":"demo_user",
"password":"123456",
"birthday":{
"year":2000,
"month":1,
"day":1
},
"email":"demo@demo.de",
"addressList":[
{
"id":4,
"street":"An der Weberei",
"number":"5",
"postalCode":"96049"
}
]
}
But with the following code by adding excludeFieldsWithoutExposeAnnotation(),
Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.create();
System.out.println(gson.toJson(user));
I get the following Json without any value in the birthday field,
{
"userName":"demo_user",
"birthday":{
},
"email":"demo@demo.de",
"addressList":[
{
"street":"An der Weberei",
"number":"5",
"postalCode":"96049"
}
]
}
Am I missing something here which is causing this problem?