Home > Mobile >  Creating a string list from RestTemplate
Creating a string list from RestTemplate

Time:11-15

I am using restTemplate to retrieve data from a url, and I get it as a List of Objects but I need a List of Strings to be able to filter it (I want to remove duplicates and change some attribute names).

This is my Template:

public static Provinces restTemplateProvince(RestTemplate restTemplate) {
        String ProvinceCommunityURL = "https://www.el-tiempo.net/api/json/v2/provincias";

        Provinces province = restTemplate.getForObject(ProvinceCommunityURL, Provinces.class);

        return province;
    }

Now I want to filter this data and show it in my own API. I'm able to show it with the following:

RestController
public class ShowcaseController {

    @Autowired
    ProvinceService provinceService;

    @GetMapping("/provinces")
    public Provinces getAllProvinces(){

      return provinceService.getAllProvinces(); 

    }
}
@Service
public class ProvinceService {

    @Autowired
    RestTemplate restTemplate;

    public Provinces getAllProvinces(){
        
        Provinces listOfProvinces = Templates.restTemplateProvince(searchList);
        return listOfProvinces;
    }
}

But I can't filter it in this list type. How could I do it?

My Province class:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Provinces {
    
    @JsonProperty("provincial")
    private List<ProvincesData> provinces;

    public Provinces(){}

    public Provinces(List<ProvincesData> provinces) {
        this.provinces = provinces;
    }

    @JsonProperty("provincial")
    public List<ProvincesData> getprovinces() {
        return provinces;
    }

    @JsonProperty("Test")
    public void setprovinces(List<ProvincesData> provinces) {
        this.provinces = provinces;
    }

}

And ProvinceData class:

@JsonIgnoreProperties(ignoreUnknown = true)

public class ProvincesData {

@JsonProperty("CODPROV")
private String codProv;

@JsonProperty("NOMBRE_PROVINCIA")
private String nomeProvincia;

@JsonProperty("CODAUTON")
private String codAuton;

@JsonProperty("COMUNIDAD_CIUDAD_AUTONOMA")
private String comunidadeCidadeAutonoma;

public ProvincesData(){
}

public ProvincesData(String codProv, String nomeProvincia, String codAuton, String comunidadeCidadeAutonoma){
    
    this.codProv = codProv;
    this.nomeProvincia = nomeProvincia;
    this.codAuton = codAuton;
    this.comunidadeCidadeAutonoma = comunidadeCidadeAutonoma; 
}

@JsonProperty("CODPROV")
public String getCodProv() {
    return codProv;
}

@JsonProperty("Test")
public void setCodProv(String codProv) {
    this.codProv = codProv;
}

public String getNomeProvincia() {
    return nomeProvincia;
}

public void setNomeProvincia(String nomeProvincia) {
    this.nomeProvincia = nomeProvincia;
}

public String getCodAuton() {
    return codAuton;
}

public void setCodAuton(String codAuton) {
    this.codAuton = codAuton;
}

public String getComunidadeCidadeAutonoma() {
    return comunidadeCidadeAutonoma;
}

public void setComunidadeCidadeAutonoma(String comunidadeCidadeAutonoma) {
    this.comunidadeCidadeAutonoma = comunidadeCidadeAutonoma;
}

}

The filter to isolate the codAuton and comunidadeCidadeAutonoma columns without repeating. If possible, build a new list with only the data I want and change de variable name so that I can then show them in my API with different titles and such.

Regards.

CodePudding user response:

Update your service to something like:

public static Provinces restTemplateProvince(RestTemplate restTemplate) {
  String ProvinceCommunityURL = "https://www.el-tiempo.net/api/json/v2/provincias";
  Provinces province = restTemplate.getForObject(ProvinceCommunityURL, Provinces.class);
  List<String> included = new ArrayList<>();
  List<ProvincesData> filtered = province.getprovinces()
    .stream().filter(p -> {
      if (included.contains(p.getCodAuton())) {
        return false;
      } else {
        included.add(p.getCodAuton());
        return true;
      }
    })
    .collect(Collectors.toList());
  province.setprovinces(filtered);
  return province;
}

Could be done more efficiently but it is probably more readable like this.

  • Related