BAEL-6076: Resolving Jackson JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token (#13495)
This commit is contained in:
parent
444cc4ac2d
commit
c1564c38a0
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.exceptions;
|
||||||
|
|
||||||
|
public class Contact {
|
||||||
|
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.baeldung.exceptions;
|
||||||
|
|
||||||
|
public class Person {
|
||||||
|
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
private String contact;
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContact() {
|
||||||
|
return contact;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContact(String contact) {
|
||||||
|
this.contact = contact;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.baeldung.exceptions;
|
||||||
|
|
||||||
|
public class PersonContact {
|
||||||
|
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
private Contact contact;
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Contact getContact() {
|
||||||
|
return contact;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContact(Contact contact) {
|
||||||
|
this.contact = contact;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,17 +3,14 @@ package com.baeldung.exceptions;
|
|||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
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 java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.exceptions.User;
|
|
||||||
import com.baeldung.exceptions.UserWithPrivateFields;
|
|
||||||
import com.baeldung.exceptions.UserWithRoot;
|
|
||||||
import com.baeldung.exceptions.Zoo;
|
|
||||||
import com.baeldung.exceptions.ZooConfigured;
|
|
||||||
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.JsonFactory;
|
import com.fasterxml.jackson.core.JsonFactory;
|
||||||
@ -82,6 +79,32 @@ public class JacksonExceptionsUnitTest {
|
|||||||
.readValue(json);
|
.readValue(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJsonObject_whenDeserializingIntoString_thenException() throws IOException {
|
||||||
|
final String json = "{\"firstName\":\"Azhrioun\",\"lastName\":\"Abderrahim\",\"contact\":{\"email\":\"azh@email.com\"}}";
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
Exception exception = assertThrows(JsonMappingException.class, () -> mapper.reader()
|
||||||
|
.forType(Person.class)
|
||||||
|
.readValue(json));
|
||||||
|
|
||||||
|
assertTrue(exception.getMessage()
|
||||||
|
.contains("Cannot deserialize value of type `java.lang.String` from Object value (token `JsonToken.START_OBJECT`)"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJsonObject_whenDeserializingIntoObject_thenDeserialize() throws IOException {
|
||||||
|
final String json = "{\"firstName\":\"Azhrioun\",\"lastName\":\"Abderrahim\",\"contact\":{\"email\":\"azh@email.com\"}}";
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
PersonContact person = mapper.reader()
|
||||||
|
.forType(PersonContact.class)
|
||||||
|
.readValue(json);
|
||||||
|
|
||||||
|
assertEquals("azh@email.com", person.getContact()
|
||||||
|
.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDefaultConstructor_whenDeserializing_thenCorrect() throws IOException {
|
public void givenDefaultConstructor_whenDeserializing_thenCorrect() throws IOException {
|
||||||
final String json = "{\"id\":1,\"name\":\"John\"}";
|
final String json = "{\"id\":1,\"name\":\"John\"}";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user