Skip to content

Instantly share code, notes, and snippets.

@romain-grecourt
Created May 29, 2019 00:45
Show Gist options
  • Save romain-grecourt/d31339e21e9cb541b1eb121e526d25d9 to your computer and use it in GitHub Desktop.
Save romain-grecourt/d31339e21e9cb541b1eb121e526d25d9 to your computer and use it in GitHub Desktop.
JSONB pojo with immutable fields
package org.example;
import java.util.UUID;
import javax.json.bind.annotation.JsonbCreator;
import javax.json.bind.annotation.JsonbProperty;
public final class Employee {
private final String id;
private final String firstName;
private final String lastName;
private final String email;
private final String phone;
private final String birthDate;
private final String title;
private final String department;
private Employee(String id, String firstName, String lastName,
String email, String phone, String birthDate,
String title, String department) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.phone = phone;
this.birthDate = birthDate;
this.title = title;
this.department = department;
}
@JsonbCreator
public static Employee of(
@JsonbProperty("firstName") String firstName,
@JsonbProperty("lastName") String lastName,
@JsonbProperty("email") String email,
@JsonbProperty("phone") String phone,
@JsonbProperty("birthDate") String birthDate,
@JsonbProperty("title") String title,
@JsonbProperty("department") String department) {
return new Employee(UUID.randomUUID().toString(), firstName,
lastName, email, phone, birthDate, title, department);
}
public String getId() {
return this.id;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
public String getEmail() {
return this.email;
}
public String getPhone() {
return this.phone;
}
public String getBirthDate() {
return this.birthDate;
}
public String getTitle() {
return this.title;
}
public String getDepartment() {
return this.department;
}
@Override
public String toString() {
return "Employee{" + "id=" + id
+ ", firstName=" + firstName
+ ", lastName=" + lastName
+ ", email=" + email
+ ", phone=" + phone
+ ", birthDate=" + birthDate
+ ", title=" + title
+ ", department=" + department
+ '}';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment