So I am new to RestAssured API automation, I was trying to create json from pojo class through object mapper though I am successfully able to create json from pojo but getting error when trying to deserialize to java object through object mapper with below error at step(List<Person> latest=map.readValue(json, new TypeReference<List<Person>>() {
});) :
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type
java.util.ArrayList<org.example.Details>from Object value (token
JsonToken.START_OBJECT) at [Source: REDACTED (
StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1767)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1541)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1488)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:396)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:252)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:28)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4899)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3846)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3829)
at org.example.CreateNestedJson.generatePayload(CreateNestedJson.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:136)
at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:658)
at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:219)
at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50)
at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:923)
at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:192)
at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
at java.util.ArrayList.forEach(ArrayList.java:1259)
at org.testng.TestRunner.privateRun(TestRunner.java:808)
at org.testng.TestRunner.run(TestRunner.java:603)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:429)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:423)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:383)
at org.testng.SuiteRunner.run(SuiteRunner.java:326)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1249)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.runSuites(TestNG.java:1092)
at org.testng.TestNG.run(TestNG.java:1060)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:65)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:105)`
My payload:
{ "person" : [ { "id" : 11, "name" : "John", "city" : "NewYork", "phone" : "7647388372", "facebook" : "JohnTheGreat" } ]
My POJO classes:
Root class
package org.example;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.List;
public class NewAPITest {
List<Person> person=new ArrayList<>();
public void setPerson(List person){
this.person=person;
}
public List<Person> getPerson(){
return person;
}
}
nested class
package org.example;
public class Person {
int id;
String name;
String city;
String phone;
String facebook;
public String getFacebook() {
return facebook;
}
public String getPhone() {
return phone;
}
public String getCity() {
return city;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setCity(String city) {
this.city = city;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setFacebook(String facebook) {
this.facebook = facebook;
}
public int getId() {
return id;
}
}
class which serialize and deserialize
package org.example;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.restassured.RestAssured;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;
public class CreateNestedJson {
@Test
public void generatePayload() throws JsonProcessingException {
Person person=new Person();
person.setId(11);
person.setCity("NewYork");
person.setFacebook("JohnTheGreat");
person.setPhone("7647388372");
person.setName("John");
List<Person>list=new ArrayList<>();
list.add(person);
NewAPITest object=new NewAPITest();
object.setPerson(list);
ObjectMapper map=new ObjectMapper();
String json=map.writerWithDefaultPrettyPrinter().writeValueAsString(object);
System.out.println(json);
**List<Person> latest=map.readValue(json, new TypeReference<List<Person>>() {
});** // this line where I am getting error
for (Person s:latest) {
System.out.println("Name " + s.getId());
System.out.println("Age " + s.getName());
System.out.println("Age " + s.getCity());
System.out.println("Age " + s.getPhone());
System.out.println("Age " + s.getFacebook());
}
}
}
I was trying to create json from pojo class through object mapper though I am successfully able to create json from pojo but getting error when trying to deserialize to java object through object mapper, it should be deserialize when I am able to serialize,
not sure what am i doing wrong?
Piyush Nautiyal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.