BAEL-5737: Fix the JsonMappingException: Can not deserialize instance of java.util.ArrayList (#13837)
This commit is contained in:
parent
4663ee7fa8
commit
61b242fcfd
|
@ -0,0 +1,26 @@
|
||||||
|
package com.baeldung.mappingexception;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Country {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private List<String> cities;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getCities() {
|
||||||
|
return cities;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCities(List<String> cities) {
|
||||||
|
this.cities = cities;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,34 +1,33 @@
|
||||||
package com.baeldung.mappingexception;
|
package com.baeldung.mappingexception;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.hamcrest.Matchers.not;
|
|
||||||
import static org.hamcrest.Matchers.notNullValue;
|
import static org.hamcrest.Matchers.notNullValue;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.junit.Assert.assertThrows;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
||||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||||
import com.fasterxml.jackson.core.JsonGenerationException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.core.JsonParseException;
|
|
||||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
|
|
||||||
public class JacksonMappingExceptionUnitTest {
|
public class JacksonMappingExceptionUnitTest {
|
||||||
|
|
||||||
@Test(expected = JsonMappingException.class)
|
@Test(expected = JsonMappingException.class)
|
||||||
public final void givenObjectHasNoAccessors_whenSerializing_thenException() throws JsonParseException, IOException {
|
public void givenObjectHasNoAccessors_whenSerializing_thenException() throws JsonProcessingException {
|
||||||
final String dtoAsString = new ObjectMapper().writeValueAsString(new MyDtoNoAccessors());
|
final String dtoAsString = new ObjectMapper().writeValueAsString(new MyDtoNoAccessors());
|
||||||
|
|
||||||
assertThat(dtoAsString, notNullValue());
|
assertThat(dtoAsString, notNullValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenObjectHasNoAccessors_whenSerializingWithPrivateFieldsVisibility_thenNoException() throws JsonParseException, IOException {
|
public void givenObjectHasNoAccessors_whenSerializingWithPrivateFieldsVisibility_thenNoException() throws JsonProcessingException {
|
||||||
final ObjectMapper objectMapper = new ObjectMapper();
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
|
objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
|
||||||
final String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessors());
|
final String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessors());
|
||||||
|
@ -39,7 +38,7 @@ public class JacksonMappingExceptionUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenObjectHasNoAccessorsButHasVisibleFields_whenSerializing_thenNoException() throws JsonParseException, IOException {
|
public void givenObjectHasNoAccessorsButHasVisibleFields_whenSerializing_thenNoException() throws JsonProcessingException {
|
||||||
final ObjectMapper objectMapper = new ObjectMapper();
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
final String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessorsAndFieldVisibility());
|
final String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessorsAndFieldVisibility());
|
||||||
|
|
||||||
|
@ -48,4 +47,30 @@ public class JacksonMappingExceptionUnitTest {
|
||||||
assertThat(dtoAsString, containsString("booleanValue"));
|
assertThat(dtoAsString, containsString("booleanValue"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJsonWithInvalidList_whenDeserializing_thenThrowException() throws JsonProcessingException {
|
||||||
|
final String json = "{\"name\":\"Netherlands\",\"cities\":{\"Amsterdam\", \"Tamassint\"}}";
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
Exception exception = assertThrows(JsonMappingException.class, () -> mapper.reader()
|
||||||
|
.forType(Country.class)
|
||||||
|
.readValue(json));
|
||||||
|
|
||||||
|
assertTrue(exception.getMessage()
|
||||||
|
.contains("Cannot deserialize value of type `java.util.ArrayList<java.lang.String>`"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJsonWithValidList_whenDeserializing_thenCorrect() throws JsonProcessingException {
|
||||||
|
final String json = "{\"name\":\"Netherlands\",\"cities\":[\"Amsterdam\", \"Tamassint\"]}";
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
Country country = mapper.reader()
|
||||||
|
.forType(Country.class)
|
||||||
|
.readValue(json);
|
||||||
|
|
||||||
|
assertEquals("Netherlands", country.getName());
|
||||||
|
assertEquals(Arrays.asList("Amsterdam", "Tamassint"), country.getCities());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue