I hit exception when using restTemplate.postForObject().
client code like below:
List < com.chen.sopan.entity.SopanItem> result = restTemplate.postForObject("http://sopanService/getResultList", param, List.class);
service code:
@RequestMapping(value = "/getResultList")
public List
List< com.chen.sopan.entity.SopanItem> resultList =....
return resultList ;
}
Can you please provide more information? Whatever is marshalling your objects is putting it into a LinkedHashMap. Maybe it's missing some configuration?
I have resolved it by using ParameterizedTypeReference.
https://stackoverflow.com/questions/19463372/classcastexception-resttemplate-returning-listlinkedhashmap-instead-of-listm
For all further dudes who struggle with this and are searching the entire web for any solution, consider that you also could transform the response type of your REST API into an array type (instead of List):
@GetMapping(value = "...")
public ResponseEntity<MyModel[]> getSomething(...) { ...
And on the client side you invoke it like this:
final ResponseEntity<MyModel[]> response = restTemplate
.getForEntity(uri, MyModel[].class);
return Arrays.asList(response.getBody());
I m unable to insert the obtained json into my db....it is showing class cast exception...in which linked hash map cannot be casted to my user entity
hi..this is my csv.java
public class CSV {
public List
File input = new File("C:\\Users\\SwedhaS\\Documents\\SametimeFileTransfers\\newPIR.csv");
File output = new File("C:\\Users\\SwedhaS\\Documents\\output.json");
CsvSchema csvSchema = CsvSchema.builder().setUseHeader(true).build();
CsvMapper csvMapper = new CsvMapper();
// Read data from CSV file
List<Object> readAll = csvMapper.readerFor(Map.class).with(csvSchema).readValues(input).readAll();
ObjectMapper mapper = new ObjectMapper();
// Write JSON formated data to output.json file
mapper.writerWithDefaultPrettyPrinter().writeValue(output, readAll);
// Write JSON formated data to stdout
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(readAll);
return readAll ;
}
this is my controller
@PostMapping("/save")
public String saveUser(@RequestBody User user) throws FileNotFoundException {
CSV read_csv = new CSV();
List
try {
userList = read_csv.csvRead();
userRepo.dbInsert(userList);
// System.out.println(userList);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
this is my service method for inserting csv file
@Override
public String dbInsert(List
// creating database
CouchDbConnector connector = new StdCouchDbConnector("demouser", dbInstance);
CouchDbImpl impl = new CouchDbImpl(connector);
connector.createDatabaseIfNotExists();
String database = connector.getDatabaseName();
System.out.println(database);
for(int i=0;i<userList.size();i++)
impl.add((User)userList.get(i));
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
Most helpful comment
For all further dudes who struggle with this and are searching the entire web for any solution, consider that you also could transform the response type of your REST API into an array type (instead of List):
And on the client side you invoke it like this: