So i'm having a elasticsearch database, which i used to store Contacts. i do the below query.
public String getAllContacts() throws IOException {
SearchResponse response = client.prepareSearch("contact").get();
return response.toString();
}
so then i get a result like this Json result from query
then i want to put the Json data in to my Contact class objects
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Date;
@JsonIgnoreProperties(ignoreUnknown = true)
@XmlRootElement
public class Contact {
private long id;
private String name;
private Date date;
public Contact() {}
public Contact(long id, String name, Date date) {
this.id = id;
this.name = name;
this.date = date;
}
public long getId() {return id;}
public void setId(long id) {this.id = id;}
public String getName() { return name;}
public void setName(String name) {this.name = name;}
public Date getDate() { return date; }
public void setDate(Date date) { this.date = date; }
}
later i But when i try to map the JSON to object using below code i get errors
ObjectMapper mapper = new ObjectMapper();
List<Contact> myObjects = mapper
.readValue(response.toString(), mapper
.getTypeFactory()
.constructCollectionType(List.class, Contact.class));
one of many errors
Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
so the problem i think is all the other stuff which comes from the query if I get a clean JSON like this {"id":"2","name";"dinu"} it works (i did test it manually) but it comes with many other stuff such as the top part of the JSON file(header meta data) (check the above JSON result image).
so i think i have 2 options:-
Option 1 :- get a clean result from elasticsearch DB, so i need to modify the search query. i don't know if that is even possible with elasticsearch. if so can someone make suggestion on how to modify the query (in java API).
Option 2 :- filter out the JSON file and remove all unwanted stuff and then make it into Contact object. i tried GSON and jackson both failed with the unfiltered file. If there is any advance or custom way to filter out let me know.
Option 3 :- i'm totally wrong and there is a better easy way to do it.Please let me know.
UPDATE Watch THIS :- https://www.youtube.com/watch?v=YgKcVBbvy2U
so i tried the above way using GSON to Serialize
then exceptions are not coming but i get the responds after serialization as "Unexpected 'd'" also in raw formate it outputs this dinu.model.HitsObject@6c79684b everytime i send a GET respond the text after @ symbol changes'.