further jackson work
This commit is contained in:
parent
62e0386a9d
commit
e088098c50
@ -0,0 +1,42 @@
|
|||||||
|
package org.baeldung.jackson;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
|
||||||
|
public class MyDtoIgnoreField {
|
||||||
|
|
||||||
|
private String stringValue;
|
||||||
|
@JsonIgnore
|
||||||
|
private int intValue;
|
||||||
|
private boolean booleanValue;
|
||||||
|
|
||||||
|
public MyDtoIgnoreField() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,6 +3,6 @@ package org.baeldung.jackson;
|
|||||||
import com.fasterxml.jackson.annotation.JsonIgnoreType;
|
import com.fasterxml.jackson.annotation.JsonIgnoreType;
|
||||||
|
|
||||||
@JsonIgnoreType
|
@JsonIgnoreType
|
||||||
public class MyBooleanMixIn {
|
public class MyMixInForString {
|
||||||
//
|
//
|
||||||
}
|
}
|
@ -7,8 +7,9 @@ import static org.junit.Assert.assertThat;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.baeldung.jackson.MyBooleanMixIn;
|
import org.baeldung.jackson.MyMixInForString;
|
||||||
import org.baeldung.jackson.MyDto;
|
import org.baeldung.jackson.MyDto;
|
||||||
|
import org.baeldung.jackson.MyDtoIgnoreField;
|
||||||
import org.baeldung.jackson.MyDtoIgnoreFieldByName;
|
import org.baeldung.jackson.MyDtoIgnoreFieldByName;
|
||||||
import org.baeldung.jackson.MyDtoIncludeNonDefault;
|
import org.baeldung.jackson.MyDtoIncludeNonDefault;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -43,7 +44,7 @@ public class JacksonSerializationUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenFieldIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
public final void givenFieldIsIgnoredByName_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
final MyDtoIgnoreFieldByName dtoObject = new MyDtoIgnoreFieldByName();
|
final MyDtoIgnoreFieldByName dtoObject = new MyDtoIgnoreFieldByName();
|
||||||
dtoObject.setBooleanValue(true);
|
dtoObject.setBooleanValue(true);
|
||||||
@ -55,10 +56,22 @@ public class JacksonSerializationUnitTest {
|
|||||||
System.out.println(dtoAsString);
|
System.out.println(dtoAsString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenFieldIsIgnoredDirectly_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
final MyDtoIgnoreField dtoObject = new MyDtoIgnoreField();
|
||||||
|
|
||||||
|
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||||
|
|
||||||
|
assertThat(dtoAsString, not(containsString("intValue")));
|
||||||
|
assertThat(dtoAsString, containsString("booleanValue"));
|
||||||
|
System.out.println(dtoAsString);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenFieldTypeIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
public final void givenFieldTypeIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
mapper.addMixInAnnotations(String.class, MyBooleanMixIn.class);
|
mapper.addMixInAnnotations(String.class, MyMixInForString.class);
|
||||||
final MyDto dtoObject = new MyDto();
|
final MyDto dtoObject = new MyDto();
|
||||||
dtoObject.setBooleanValue(true);
|
dtoObject.setBooleanValue(true);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user