BAEL-5830 Add code for setting default values for null fields with Jackson (#14265)
This commit is contained in:
parent
4ad0de6842
commit
9bbd08c6a8
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.jackson.defaultvalues;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonSetter;
|
||||
|
||||
public class JsonSetterDefaultValue {
|
||||
|
||||
private String required;
|
||||
private String optional = "valueIfMissingEntirely";
|
||||
|
||||
@JsonSetter("optional")
|
||||
public void setOptional(String optional){
|
||||
if(optional == null){
|
||||
this.optional = "valueIfNull";
|
||||
}
|
||||
}
|
||||
|
||||
public String getRequired() {
|
||||
return required;
|
||||
}
|
||||
|
||||
public String getOptional() {
|
||||
return optional;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NonAnnotatedDefaultValue{" + "required='" + required + '\'' + ", optional='" + optional + '\'' + '}';
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.jackson.defaultvalues;
|
||||
|
||||
public class NonAnnotatedDefaultValue {
|
||||
|
||||
private String required;
|
||||
private String optional = "defaultValue";
|
||||
|
||||
public String getRequired() {
|
||||
return required;
|
||||
}
|
||||
|
||||
public String getOptional() {
|
||||
return optional;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NonAnnotatedDefaultValue{" + "required='" + required + '\'' + ", optional='" + optional + '\'' + '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.jackson.defaultvalues;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonSetter;
|
||||
import com.fasterxml.jackson.annotation.Nulls;
|
||||
|
||||
public class NullsSkipDefaultValue {
|
||||
|
||||
private String required;
|
||||
@JsonSetter(nulls = Nulls.SKIP)
|
||||
private String optional = "defaultValue";
|
||||
|
||||
public void setOptional(String optional){}
|
||||
|
||||
public String getRequired() {
|
||||
return required;
|
||||
}
|
||||
|
||||
public String getOptional() {
|
||||
return optional;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NonAnnotatedDefaultValue{" + "required='" + required + '\'' + ", optional='" + optional + '\'' + '}';
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.jackson.defaultvalues;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class DefaultValuesUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAClassWithADefaultValue_whenReadingJsonWithoutOptionalValue_thenExpectDefaultValueInResult() throws JsonProcessingException {
|
||||
String noOptionalField = "{\"required\": \"value\"}";
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
NonAnnotatedDefaultValue createdObject = objectMapper.readValue(noOptionalField, NonAnnotatedDefaultValue.class);
|
||||
assert(createdObject.getRequired()).equals("value");
|
||||
assert(createdObject.getOptional()).equals("defaultValue");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAClassWithAJsonSetter_whenReadingJsonWithNullOptionalValue_thenExpectDefaultValueInResult() throws JsonProcessingException {
|
||||
String nullOptionalField = "{\"required\": \"value\", \"optional\": null}";
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonSetterDefaultValue createdObject = objectMapper.readValue(nullOptionalField, JsonSetterDefaultValue.class);
|
||||
assert(createdObject.getRequired()).equals("value");
|
||||
assert(createdObject.getOptional()).equals("valueIfNull");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAClassWithAJsonSetterNullsSkip_whenReadingJsonWithNullOptionalValue_thenExpectDefaultValueInResult() throws JsonProcessingException {
|
||||
String nullOptionalField = "{\"required\": \"value\", \"optional\": null}";
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
NullsSkipDefaultValue createdObject = objectMapper.readValue(nullOptionalField, NullsSkipDefaultValue.class);
|
||||
assert(createdObject.getRequired()).equals("value");
|
||||
assert(createdObject.getOptional()).equals("defaultValue");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue