Spring-cloud-netflix: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.chen.sopan.entity.SopanItem

Created on 12 Feb 2017  Â·  4Comments  Â·  Source: spring-cloud/spring-cloud-netflix

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 getResultList(HttpServletRequest request, @RequestBody SoPanInfoParam param) {

    List< com.chen.sopan.entity.SopanItem> resultList =....
    return resultList ;
}

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):

  @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());

All 4 comments

Can you please provide more information? Whatever is marshalling your objects is putting it into a LinkedHashMap. Maybe it's missing some configuration?

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 csvRead()throws Exception {

        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 userList = null;

    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 userList) {
try {
CouchDbInstance dbInstance = getconnection();

        // 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;




}
Was this page helpful?
0 / 5 - 0 ratings