为了从json获取数据,我使用了RestTemplate,它可以获取我的所有数据并在localhost上显示它。我得到的arrayList,我想现在过滤只获得一些数据并把它放在一个新的arrayList中,但是当我尝试运行它时,它给了我“java.lang.ClassCastException:java.util.LinkedHashMap不能转换为model.Artists” 我应该以其他方式获取Json数据,还是过滤功能需要一些转换?
模型类 包装模型; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@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;
}
服务类
@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);
}
}
控制器类
{{1}}