diff --git a/spring-5/src/main/java/com/baeldung/jsonb/Person.java b/spring-5/src/main/java/com/baeldung/jsonb/Person.java index 99ebd54f0b..7a54b37574 100644 --- a/spring-5/src/main/java/com/baeldung/jsonb/Person.java +++ b/spring-5/src/main/java/com/baeldung/jsonb/Person.java @@ -10,6 +10,7 @@ import javax.json.bind.annotation.JsonbTransient; public class Person { + private int id; @JsonbProperty("person-name") private String name; @JsonbProperty(nillable = true) @@ -23,8 +24,9 @@ public class Person { public Person() { } - public Person(String name, String email, int age, LocalDate registeredDate, BigDecimal salary) { + public Person(int id, String name, String email, int age, LocalDate registeredDate, BigDecimal salary) { super(); + this.id = id; this.name = name; this.email = email; this.age = age; @@ -32,6 +34,14 @@ public class Person { this.salary = salary; } + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + public int getAge() { return age; } @@ -76,7 +86,9 @@ public class Person { @Override public String toString() { StringBuilder builder = new StringBuilder(); - builder.append("Person [name="); + builder.append("Person [id="); + builder.append(id); + builder.append(", name="); builder.append(name); builder.append(", email="); builder.append(email); @@ -94,11 +106,7 @@ public class Person { public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + age; - result = prime * result + ((email == null) ? 0 : email.hashCode()); - result = prime * result + ((name == null) ? 0 : name.hashCode()); - result = prime * result + ((registeredDate == null) ? 0 : registeredDate.hashCode()); - result = prime * result + ((salary == null) ? 0 : salary.hashCode()); + result = prime * result + id; return result; } @@ -111,27 +119,7 @@ public class Person { if (getClass() != obj.getClass()) return false; Person other = (Person) obj; - if (age != other.age) - return false; - if (email == null) { - if (other.email != null) - return false; - } else if (!email.equals(other.email)) - return false; - if (name == null) { - if (other.name != null) - return false; - } else if (!name.equals(other.name)) - return false; - if (registeredDate == null) { - if (other.registeredDate != null) - return false; - } else if (!registeredDate.equals(other.registeredDate)) - return false; - if (salary == null) { - if (other.salary != null) - return false; - } else if (!salary.equals(other.salary)) + if (id != other.id) return false; return true; } diff --git a/spring-5/src/main/java/com/baeldung/jsonb/PersonController.java b/spring-5/src/main/java/com/baeldung/jsonb/PersonController.java index 75fcd1e2cc..e216a282eb 100644 --- a/spring-5/src/main/java/com/baeldung/jsonb/PersonController.java +++ b/spring-5/src/main/java/com/baeldung/jsonb/PersonController.java @@ -26,12 +26,12 @@ public class PersonController { public void init() { // @formatter:off personRepository = new ArrayList<>(Arrays.asList( - new Person("Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)), - new Person("Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)), - new Person("Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)), - new Person("Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)), - new Person("Mark", "mark@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1200)), - new Person("Julia", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)))); + new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)), + new Person(2, "Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)), + new Person(3, "Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)), + new Person(4, "Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)), + new Person(5, "Mark", "mark@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1200)), + new Person(6, "Julia", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)))); // @formatter:on } diff --git a/spring-5/src/test/java/com/baeldung/jsonb/JsonbIntegrationTest.java b/spring-5/src/test/java/com/baeldung/jsonb/JsonbIntegrationTest.java index a31a16d610..756b303f3b 100644 --- a/spring-5/src/test/java/com/baeldung/jsonb/JsonbIntegrationTest.java +++ b/spring-5/src/test/java/com/baeldung/jsonb/JsonbIntegrationTest.java @@ -30,15 +30,14 @@ public class JsonbIntegrationTest { ResponseEntity response = template.withBasicAuth(username, password) .getForEntity("/person/1", Person.class); Person person = response.getBody(); - assertTrue(person.equals(new Person("Jhon", "jhon1@test.com", 0, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500.0)))); + assertTrue(person.equals(new Person(2, "Jhon", "jhon1@test.com", 0, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500.0)))); } @Test public void whenSendPostAPerson_thenGetOkStatus() { ResponseEntity response = template.withBasicAuth(username, password) - .postForEntity("/person", "{\"birthDate\":\"07-09-2017\",\"email\":\"jhon1@test.com\",\"person-name\":\"Jhon\"}", Boolean.class); - boolean value = response.getBody(); - assertTrue(true == value); + .postForEntity("/person", "{\"birthDate\":\"07-09-2017\",\"email\":\"jhon1@test.com\",\"person-name\":\"Jhon\",\"id\":10}", Boolean.class); + assertTrue(response.getBody()); } } diff --git a/spring-5/src/test/java/com/baeldung/jsonb/JsonbTest.java b/spring-5/src/test/java/com/baeldung/jsonb/JsonbTest.java index 7133999551..4caab86f7d 100644 --- a/spring-5/src/test/java/com/baeldung/jsonb/JsonbTest.java +++ b/spring-5/src/test/java/com/baeldung/jsonb/JsonbTest.java @@ -22,15 +22,15 @@ public class JsonbTest { @Test public void givenPersonObject_whenSerializeWithJsonb_thenGetPersonJson() { - Person person = new Person("Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000)); + Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000)); String jsonPerson = jsonb.toJson(person); - assertTrue("{\"email\":\"jhon@test.com\",\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}".equals(jsonPerson)); + assertTrue("{\"email\":\"jhon@test.com\",\"id\":1,\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}".equals(jsonPerson)); } @Test public void givenPersonJson_whenDeserializeWithJsonb_thenGetPersonObject() { - Person person = new Person("Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0)); - String jsonPerson = "{\"email\":\"jhon@test.com\",\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}"; + Person person = new Person(1, "Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0)); + String jsonPerson = "{\"email\":\"jhon@test.com\",\"id\":1,\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}"; assertTrue(jsonb.fromJson(jsonPerson, Person.class) .equals(person)); }