Skip to content

Commit 4c6e4c1

Browse files
author
juan
committed
Update Person class to use property "id" like identifier.
1 parent 77aa65b commit 4c6e4c1

4 files changed

Lines changed: 29 additions & 42 deletions

File tree

‎spring-5/src/main/java/com/baeldung/jsonb/Person.java‎

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
public class Person {
1212

13+
private int id;
1314
@JsonbProperty("person-name")
1415
private String name;
1516
@JsonbProperty(nillable = true)
@@ -23,15 +24,24 @@ public class Person {
2324
public Person() {
2425
}
2526

26-
public Person(String name, String email, int age, LocalDate registeredDate, BigDecimal salary) {
27+
public Person(int id, String name, String email, int age, LocalDate registeredDate, BigDecimal salary) {
2728
super();
29+
this.id = id;
2830
this.name = name;
2931
this.email = email;
3032
this.age = age;
3133
this.registeredDate = registeredDate;
3234
this.salary = salary;
3335
}
3436

37+
public int getId() {
38+
return id;
39+
}
40+
41+
public void setId(int id) {
42+
this.id = id;
43+
}
44+
3545
public int getAge() {
3646
return age;
3747
}
@@ -76,7 +86,9 @@ public void setRegisteredDate(LocalDate registeredDate) {
7686
@Override
7787
public String toString() {
7888
StringBuilder builder = new StringBuilder();
79-
builder.append("Person [name=");
89+
builder.append("Person [id=");
90+
builder.append(id);
91+
builder.append(", name=");
8092
builder.append(name);
8193
builder.append(", email=");
8294
builder.append(email);
@@ -94,11 +106,7 @@ public String toString() {
94106
public int hashCode() {
95107
final int prime = 31;
96108
int result = 1;
97-
result = prime * result + age;
98-
result = prime * result + ((email == null) ? 0 : email.hashCode());
99-
result = prime * result + ((name == null) ? 0 : name.hashCode());
100-
result = prime * result + ((registeredDate == null) ? 0 : registeredDate.hashCode());
101-
result = prime * result + ((salary == null) ? 0 : salary.hashCode());
109+
result = prime * result + id;
102110
return result;
103111
}
104112

@@ -111,27 +119,7 @@ public boolean equals(Object obj) {
111119
if (getClass() != obj.getClass())
112120
return false;
113121
Person other = (Person) obj;
114-
if (age != other.age)
115-
return false;
116-
if (email == null) {
117-
if (other.email != null)
118-
return false;
119-
} else if (!email.equals(other.email))
120-
return false;
121-
if (name == null) {
122-
if (other.name != null)
123-
return false;
124-
} else if (!name.equals(other.name))
125-
return false;
126-
if (registeredDate == null) {
127-
if (other.registeredDate != null)
128-
return false;
129-
} else if (!registeredDate.equals(other.registeredDate))
130-
return false;
131-
if (salary == null) {
132-
if (other.salary != null)
133-
return false;
134-
} else if (!salary.equals(other.salary))
122+
if (id != other.id)
135123
return false;
136124
return true;
137125
}

‎spring-5/src/main/java/com/baeldung/jsonb/PersonController.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ public class PersonController {
2626
public void init() {
2727
// @formatter:off
2828
personRepository = new ArrayList<>(Arrays.asList(
29-
new Person("Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
30-
new Person("Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)),
31-
new Person("Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
32-
new Person("Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)),
33-
new Person("Mark", "mark@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1200)),
34-
new Person("Julia", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000))));
29+
new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
30+
new Person(2, "Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)),
31+
new Person(3, "Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
32+
new Person(4, "Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)),
33+
new Person(5, "Mark", "mark@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1200)),
34+
new Person(6, "Julia", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000))));
3535
// @formatter:on
3636

3737
}

‎spring-5/src/test/java/com/baeldung/jsonb/JsonbIntegrationTest.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,14 @@ public void givenId_whenUriIsPerson_thenGetPerson() {
3030
ResponseEntity<Person> response = template.withBasicAuth(username, password)
3131
.getForEntity("/person/1", Person.class);
3232
Person person = response.getBody();
33-
assertTrue(person.equals(new Person("Jhon", "jhon1@test.com", 0, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500.0))));
33+
assertTrue(person.equals(new Person(2, "Jhon", "jhon1@test.com", 0, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500.0))));
3434
}
3535

3636
@Test
3737
public void whenSendPostAPerson_thenGetOkStatus() {
3838
ResponseEntity<Boolean> response = template.withBasicAuth(username, password)
39-
.postForEntity("/person", "{\"birthDate\":\"07-09-2017\",\"email\":\"jhon1@test.com\",\"person-name\":\"Jhon\"}", Boolean.class);
40-
boolean value = response.getBody();
41-
assertTrue(true == value);
39+
.postForEntity("/person", "{\"birthDate\":\"07-09-2017\",\"email\":\"jhon1@test.com\",\"person-name\":\"Jhon\",\"id\":10}", Boolean.class);
40+
assertTrue(response.getBody());
4241
}
4342

4443
}

‎spring-5/src/test/java/com/baeldung/jsonb/JsonbTest.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ public void setup() {
2222

2323
@Test
2424
public void givenPersonObject_whenSerializeWithJsonb_thenGetPersonJson() {
25-
Person person = new Person("Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000));
25+
Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000));
2626
String jsonPerson = jsonb.toJson(person);
27-
assertTrue("{\"email\":\"jhon@test.com\",\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}".equals(jsonPerson));
27+
assertTrue("{\"email\":\"jhon@test.com\",\"id\":1,\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}".equals(jsonPerson));
2828
}
2929

3030
@Test
3131
public void givenPersonJson_whenDeserializeWithJsonb_thenGetPersonObject() {
32-
Person person = new Person("Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0));
33-
String jsonPerson = "{\"email\":\"jhon@test.com\",\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}";
32+
Person person = new Person(1, "Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0));
33+
String jsonPerson = "{\"email\":\"jhon@test.com\",\"id\":1,\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}";
3434
assertTrue(jsonb.fromJson(jsonPerson, Person.class)
3535
.equals(person));
3636
}

0 commit comments

Comments
 (0)