BAEL-7277: Disable fail_on_empty_beans in Jackson (#15394)
This commit is contained in:
parent
51f2ed495b
commit
d8bbd3d027
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.exceptions;
|
||||
|
||||
public class EmptyObject {
|
||||
|
||||
}
|
|
@ -2,13 +2,15 @@ package com.baeldung.mappingexception;
|
|||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.baeldung.exceptions.EmptyObject;
|
||||
import com.baeldung.exceptions.Person;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
||||
|
@ -73,4 +75,18 @@ public class JacksonMappingExceptionUnitTest {
|
|||
assertEquals(Arrays.asList("Amsterdam", "Tamassint"), country.getCities());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyBean_whenSerializing_thenCorrect() throws JsonProcessingException {
|
||||
// Create an ObjectMapper
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
// Disable fail_on_empty_beans during serialization
|
||||
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
||||
// Create an empty object
|
||||
EmptyObject emptyObject = new EmptyObject();
|
||||
// Serialize the empty object
|
||||
String json = objectMapper.writeValueAsString(emptyObject);
|
||||
// Verify that serialization is successful
|
||||
assertEquals("{}", json);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue