I have tried to read external json file and write to Database. I am getting the error below "not able to deseriliaze" while reading the json file using objectmapper.
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<com.vir.in.model.SmartPay>` out of START_OBJECT token at [Source: (BufferedInputStream); line: 1, column: 1]
Json file
{
"header":{
"requestRefNo":"1234ERT",
"totalPDPICount":"2"
},
"files":[
{object},
{object}
]
}
Code:
@Bean
CommandLineRunner runner(SmartPayService service) throws IOException {
return args->{
//read json and write to DB
ObjectMapper mapper=new ObjectMapper();
TypeReference<List<SmartPay>> typeReference=new TypeReference<List<SmartPay>>() {};
InputStream inputStream=typeReference.getClass().getResourceAsStream("/json/trades.json");
List<SmartPay> s=mapper.readValue(inputStream, typeReference);
service.save(s);
System.out.println("Saved !");
};
}
}
service
@Autowired
private JsonRepository repo;
public Iterable<SmartPay> save(List<SmartPay> smartPay) {
return repo.saveAll(smartPay);
}