minor jackson upgrade, using jackson filters
This commit is contained in:
parent
ca3e353165
commit
ca5d33bb9a
|
@ -104,7 +104,7 @@
|
|||
<mysql-connector-java.version>5.1.27</mysql-connector-java.version>
|
||||
|
||||
<!-- marshalling -->
|
||||
<jackson.version>2.2.3</jackson.version>
|
||||
<jackson.version>2.3.0</jackson.version>
|
||||
|
||||
<!-- logging -->
|
||||
<org.slf4j.version>1.7.5</org.slf4j.version>
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
package org.baeldung.jackson.ignore;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
|
||||
@JsonFilter("myFilter")
|
||||
public class MyDtoWithFilter {
|
||||
|
||||
private String stringValue;
|
||||
private int intValue;
|
||||
private boolean booleanValue;
|
||||
|
||||
public MyDtoWithFilter() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyDtoWithFilter(final String stringValue, final int intValue, final boolean booleanValue) {
|
||||
super();
|
||||
|
||||
this.stringValue = stringValue;
|
||||
this.intValue = intValue;
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public String getStringValue() {
|
||||
return stringValue;
|
||||
}
|
||||
|
||||
public void setStringValue(final String stringValue) {
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
public int getIntValue() {
|
||||
return intValue;
|
||||
}
|
||||
|
||||
public void setIntValue(final int intValue) {
|
||||
this.intValue = intValue;
|
||||
}
|
||||
|
||||
public boolean isBooleanValue() {
|
||||
return booleanValue;
|
||||
}
|
||||
|
||||
public void setBooleanValue(final boolean booleanValue) {
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
}
|
|
@ -11,11 +11,15 @@ import org.baeldung.jackson.ignore.MyDto;
|
|||
import org.baeldung.jackson.ignore.MyDtoIgnoreField;
|
||||
import org.baeldung.jackson.ignore.MyDtoIgnoreFieldByName;
|
||||
import org.baeldung.jackson.ignore.MyDtoIncludeNonDefault;
|
||||
import org.baeldung.jackson.ignore.MyDtoWithFilter;
|
||||
import org.baeldung.jackson.ignore.MyMixInForString;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class JacksonSerializationUnitTest {
|
||||
|
@ -83,6 +87,23 @@ public class JacksonSerializationUnitTest {
|
|||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenTypeHasFilterThatIgnoresIntsOver10_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("intValue");
|
||||
final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter);
|
||||
|
||||
final MyDtoWithFilter dtoObject = new MyDtoWithFilter();
|
||||
dtoObject.setIntValue(12);
|
||||
|
||||
final String dtoAsString = mapper.writer(filters).writeValueAsString(dtoObject);
|
||||
|
||||
assertThat(dtoAsString, not(containsString("intValue")));
|
||||
assertThat(dtoAsString, containsString("booleanValue"));
|
||||
assertThat(dtoAsString, containsString("stringValue"));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
// tests - multiple entities to json
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue