0

I am trying to store the response I got in JSON in my database in spring boot. But when it shows JSON Parse Error

This is my JSON response

[
{
id: 1,
name: "Bilbo Baggins",
location: "india",
email: "jba2hba.com",
dateOfBirth: "2020-12-21T13:13:38.000+00:00"
},
{
id: 2,
name: "Frodo Baggins",
location: "bhutan",
email: "jhb@hbh.com",
dateOfBirth: "2020-12-21T13:13:38.000+00:00"
}
]

My Employee model

    @Entity
    public class Employee {
        
        private @Id @GeneratedValue( strategy = GenerationType.AUTO ) Long id;
        private String name;
        private String location;
        private String email;
        private Date dateOfBirth;
    
        public Employee() {
        }
    
        public Employee(String name, String location, String email, Date date) {
    
            this.name = name;
            this.location = location;
            this.email = email;
            this.dateOfBirth = date;
        }
//getters and setters

My getEmployees() method

public void getEmployees() {
        
        RestTemplate restTemplate = new RestTemplate();
        Employee result = restTemplate.getForObject(Constants.URI, Employee.class);
        System.out.println(result);
        }

This is the error I am getting

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.nagarro.hrLogin.entity.Employee` out of START_ARRAY token
 at [Source: (PushbackInputStream); line: 1, column: 1]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59) ~[jackson-databind-2.11.3.jar:2.11.3]
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1468) ~[jackson-databind-2.11.3.jar:2.11.3]
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1242) ~[jackson-databind-2.11.3.jar:2.11.3]
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1190) ~[jackson-databind-2.11.3.jar:2.11.3]
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeFromArray(BeanDeserializer.java:604) ~[jackson-databind-2.11.3.jar:2.11.3]
//

I am not able to figure what might be causing the error if anybody could suggest me somethiing it would be really helpful

Akhil Garg
  • 89
  • 9

3 Answers3

3

you simply try to deserialize a list to an object.

you can go through ParameterizedTypeReference as below :

ResponseEntity<List<Employee>> response = this.restTemplate.exchange("URL", HttpMethod.GET,null,new ParameterizedTypeReference<List<Employee>>(){});

on the other hand, i think externalizing the entity itself is an anti pattern, try to see the DTO design pattern.

hope that would help you

the DTO is Data transfer object, it is an intermediate object which takes only the data of the entity and keeps a separation between the objects dedicated to the database and the objects to be externalized.

for the first case: try to do a simple exercise, for your entity employed add a bidirectional OneToMany relation with another entity and create a controller, you will have a recursivity exception the call between the entities.

second case: you have to send an employee object to 2 applications, one request only the name, the other only the first name, in this case, you will have one or more output DTO with mapper (like example mapstruct or dozer) for have an output to the requesting applications.

Omar Amaoun
  • 526
  • 1
  • 3
  • 20
1

You list contains two employees and you try to map them to one. The best would be to wrap them in a class that contains a list. Something like:

public class EmployeeList {

List<Employee> employees=new ArrayList<>();
//getters setters
}

And then use

Employee result = restTemplate.getForObject(Constants.URI, EmployeeList.class);

Also you may consider another layer of objects between the entity and the frontend. Serializing/deserializing entities will lead into problems especially when they include relationships between eachother.

Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
  • hi thanks for the help can you explain what does it mean "another layer of objects between the entity and the frontend"? – Akhil Garg Dec 21 '20 at 14:16
0

This exception is thrown by Jackson mapper as it's expecting an Object {}, but you send an array []. You can easily solve this by replacing Employee with Employees[] in the definition of your endpoint. See more here: Get list of JSON objects with Spring RestTemplate Cannot deserialize instance of object out of START_ARRAY token in Spring 3 REST Webservice

Olaru Alina
  • 458
  • 4
  • 8