1

To get data from json I used RestTemplate and it works for getting all my data and displaying it on the localhost. The arrayList I got, I want to filter now to get only some data and put it in a new arrayList, but when I try to run it, it gives me "java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to model.Artists" Should I get the Json data in other way, or the filtering function needs some casting?

The model class package model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Artists {
private String ArtistName;
private String Stage;
private String Day;
private String Hour;

public Artists(){
}

public String getArtistName() {
    return ArtistName;
}

public void setArtistName(String artistName) {
    ArtistName = artistName;
}

public String getStage() {
    return Stage;
}

public void setStage(String stage) {
    Stage = stage;
}

public String getDay() {
    return Day;
}

public void setDay(String day) {
    Day = day;
}

public String getHour() {
    return Hour;
}

public void setHour(String hour) {
    Hour = hour;
}

@Override
public String toString() {
    return "Artists [ArtistName=" + ArtistName + ", Stage=" + Stage + ", Day=" + Day + ", Hour=" + Hour + "]";
}


}

The service class

@Service
public class ArtistsWrapper {
 private List<Artists> artists;
 private final String url ="some_random_website";
    /**
     * @return the artists
     */

    public List<Artists> getArtists() {
        RestTemplate restTemplate=new RestTemplate();
        this.artists=restTemplate.getForObject(url, List.class);

        return artists;
    }

   public List<Artists> getArtistsByStage(String stage)
   {

       System.out.println(artists.toString());
       List<Artists> result = new ArrayList<Artists>();
       for(Artists a: artists)
       {
           if(stage.equals(a.getStage()))
               System.out.println(a.toString()+"\n");
               result.add(a);
       }

       return result;
   }

The controller class

@RestController
@RequestMapping("/api")
public class ArtistsController {

@Autowired
ArtistsWrapper aw;

/*@RequestMapping("/artists")
public List<Artists> artists() {
    return aw.getArtists();
}
*/
@RequestMapping("/artists")
public List<Artists> artistsByStage(@RequestParam(value="stage", defaultValue="Main") String name) {
    return aw.getArtistsByStage(name);
}


}
OanaV.
  • 81
  • 2
  • 7
  • Did you try something like this [answer SO](https://stackoverflow.com/questions/6349421/how-to-use-jackson-to-deserialise-an-array-of-objects)? It seems another java's generics problem – fedemengo May 06 '18 at 19:39
  • if the class represents one person, why is it called `Artists`? very counter intuitive – Sharon Ben Asher May 07 '18 at 11:38

0 Answers0