Created
November 23, 2020 09:05
-
-
Save dariahervieux/b59eb46515d59b003e80ea9b887b6556 to your computer and use it in GitHub Desktop.
Spring-Jackson: deserialize a "value"(immutable) object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | |
import lombok.*; | |
/** | |
* Value object with final fields,modified by @Value lombock annotation. | |
* Jackson will deserialize the object using the builder, generated by @Builder lombock annotation. | |
*/ | |
@Value | |
@Builder | |
@JsonDeserialize(builder = ValueObject1.ValueObject1Builder.class) | |
public class ValueObject1 { | |
private String field1; | |
private int field2; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | |
/** | |
* Value object with final fields without lombock usage. | |
* Jackson will deserialize the object using the builder. | |
*/ | |
@JsonDeserialize(builder = ValueObject2.ValueObject2Builder.class) | |
public final class ValueObject2 { | |
private final String field1; | |
private final int field2; | |
ValueObject2(String field1, int field2) { | |
this.field1 = field1; | |
this.field2 = field2; | |
} | |
public static ValueObject2Builder builder() { | |
return new ValueObject2Builder(); | |
} | |
public String getField1() { | |
return this.field1; | |
} | |
public int getField2() { | |
return this.field2; | |
} | |
public boolean equals(final Object o) { | |
if (o == this) { | |
return true; | |
} | |
if (!(o instanceof ValueObject2)) { | |
return false; | |
} | |
final ValueObject2 other = (ValueObject2) o; | |
final Object this$field1 = this.getField1(); | |
final Object other$field1 = other.getField1(); | |
if (this$field1 == null ? other$field1 != null : !this$field1.equals(other$field1)) { | |
return false; | |
} | |
if (this.getField2() != other.getField2()) { | |
return false; | |
} | |
return true; | |
} | |
public int hashCode() { | |
final int PRIME = 59; | |
int result = 1; | |
final Object $field1 = this.getField1(); | |
result = result * PRIME + ($field1 == null ? 43 : $field1.hashCode()); | |
result = result * PRIME + this.getField2(); | |
return result; | |
} | |
public String toString() { | |
return "ValueObject(field1=" + this.getField1() + ", field2=" + this.getField2() + ")"; | |
} | |
public static class ValueObject2Builder { | |
private String field1; | |
private int field2; | |
ValueObject2Builder() { | |
} | |
public ValueObject2Builder field1(String field1) { | |
this.field1 = field1; | |
return this; | |
} | |
public ValueObject2Builder field2(int field2) { | |
this.field2 = field2; | |
return this; | |
} | |
public ValueObject2 build() { | |
return new ValueObject2(field1, field2); | |
} | |
public String toString() { | |
return "ValueObject.ValueObjectBuilder(field1=" + this.field1 + ", field2=" + this.field2 + ")"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment