BAEL-5890 - Jackson's Deserialization with Lombok (#13073)
* BAEL-4706 - Spring Boot with Spring Batch * BAEL-3948 - Fix test(s) in spring-batch which leaves repository.sqlite changed * BAEL-4736 - Convert JSONArray to List of Object using camel-jackson * BAEL-4756 - Mockito MockSettings * BAEL-4756 - Mockito MockSettings - fix spelling * BAEL-2674 - Upgrade the Okhttp article * BAEL-4204 - Adding Interceptors in OkHTTP * BAEL-4836 - Mocking Static Methods with Mockito * BAEL-4205 - A Guide to Events in OkHTTP * BAEL-5408 - Update Camel version in spring-boot-camel module * BAEL-5234 - Apache Camel Routes Testing in Spring Boot * BAEL-5234 - Apache Camel Routes Testing in Spring Boot * BAEL-5237 - Apache Camel Conditional Routing * BAEL-5236 - Apache Camel Exception Handling * BAEL-5890 - Jackson's Deserialization with Lombok * BAEL-5890 - Jackson's Deserialization with Lombok * BAEL-5890 - Jackson's Deserialization with Lombok * BAEL-5890 - Jackson's Deserialization with Lombok * BAEL-5890 - Jackson's Deserialization with Lombok Co-authored-by: Jonathan Cook <jcook@sciops.esa.int>
This commit is contained in:
parent
6f29cf4dbf
commit
d1e0a4cefe
|
@ -1,2 +1,2 @@
|
|||
config.stopBubbling = true
|
||||
lombok.experimental.flagUsage = warning
|
||||
lombok.experimental.flagUsage = warning
|
|
@ -20,6 +20,15 @@
|
|||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<jackson.version>2.14.1</jackson.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.lombok.jackson;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder(builderClassName = "EmployeeBuilder")
|
||||
@JsonDeserialize(builder = Employee.EmployeeBuilder.class)
|
||||
@AllArgsConstructor
|
||||
public class Employee {
|
||||
|
||||
private int identity;
|
||||
private String firstName;
|
||||
|
||||
@JsonPOJOBuilder(buildMethodName = "createEmployee", withPrefix = "construct")
|
||||
public static class EmployeeBuilder {
|
||||
|
||||
private int idValue;
|
||||
private String nameValue;
|
||||
|
||||
public EmployeeBuilder constructId(int id) {
|
||||
idValue = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EmployeeBuilder constructName(String name) {
|
||||
nameValue = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Employee createEmployee() {
|
||||
return new Employee(idValue, nameValue);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.lombok.jackson;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.extern.jackson.Jacksonized;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@Jacksonized
|
||||
public class Fruit {
|
||||
|
||||
private String name;
|
||||
private int id;
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.lombok.jackson;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
class LombokWithJacksonEmployeeUnitTest {
|
||||
|
||||
@Test
|
||||
void withEmployeeObject_thenSerializeSucessfully() throws IOException {
|
||||
Employee employee = Employee.builder()
|
||||
.identity(5)
|
||||
.firstName("Bob")
|
||||
.build();
|
||||
|
||||
String json = newObjectMapper().writeValueAsString(employee);
|
||||
assertEquals("{\"identity\":5,\"firstName\":\"Bob\"}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withEmployeeJSON_thenDeserializeSucessfully() throws IOException {
|
||||
String json = "{\"id\":5,\"name\":\"Bob\"}";
|
||||
Employee employee = newObjectMapper().readValue(json, Employee.class);
|
||||
|
||||
assertEquals(5, employee.getIdentity());
|
||||
assertEquals("Bob", employee.getFirstName());
|
||||
}
|
||||
|
||||
private ObjectMapper newObjectMapper() {
|
||||
return new ObjectMapper();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.lombok.jackson;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
class LombokWithJacksonFruitUnitTest {
|
||||
|
||||
@Test
|
||||
void withFruitObject_thenSerializeSucessfully() throws IOException {
|
||||
Fruit fruit = Fruit.builder()
|
||||
.id(101)
|
||||
.name("Apple")
|
||||
.build();
|
||||
|
||||
String json = newObjectMapper().writeValueAsString(fruit);
|
||||
assertEquals("{\"name\":\"Apple\",\"id\":101}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withFruitJSON_thenDeserializeSucessfully() throws IOException {
|
||||
String json = "{\"name\":\"Apple\",\"id\":101}";
|
||||
Fruit fruit = newObjectMapper().readValue(json, Fruit.class);
|
||||
assertEquals(new Fruit("Apple", 101), fruit);
|
||||
}
|
||||
|
||||
private ObjectMapper newObjectMapper() {
|
||||
return new ObjectMapper();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue