Skip to content
Advertisement

RestTemplate parsing from Response entity to an Object

First I created this method to get me a Pokemon by Id and parse it to a POJO class using RestTemplate.

//Service Class   
public PokemonResponseTemplateVO getPokemonFromApi(Long pokemonId) {
    log.info("Find Pokemon From Api Service Call");
    PokemonResponseTemplateVO vo = new PokemonResponseTemplateVO();
    Pokemon pokemon = restTemplate.getForObject(GET_POKEMONS_BY_ID_API + pokemonId, Pokemon.class);
    vo.setPokemon(pokemon);
    return vo;
}

//Controller Class
@GetMapping("findByIdApi/{id}")
public PokemonResponseTemplateVO getPokemonFromApi(@PathVariable("id") Long pokemonId) {
    log.info("Get Pokemon From Api Controller Call");
    return pokemonService.getPokemonFromApi(pokemonId);
    
}

//POJO
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PokemonResponseTemplateVO {

private Pokemon pokemon;

}

So far everything works fine, but now I have to parse a list of Pokemons and I can’t find a way to do the same thing with a list of objects. I already have the response correct but cant merge it to my POJO. If I change to String as the line that is commented I get the response but not the way I want it using the POJO.

//Service Class
public ResponseEntity<PokemonUrl> findAllPokemonsApi() {
    log.info("Find All Pokemon From Api Service Call");
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
    //ResponseEntity<String> result = template.exchange(GET_ALL_POKEMONS_API, HttpMethod.GET, entity, String.class);
    ResponseEntity<PokemonUrl> response = restTemplate.exchange(GET_ALL_POKEMONS_API, HttpMethod.GET, entity, PokemonUrl.class);

    return response;
}

//Controller
@GetMapping("findAllPokemons/")
public ResponseEntity<PokemonUrl> findAllPokemonsApi() {
    log.info("Get Pokemon From Api Controller Call");
    return pokemonService.findAllPokemonsApi();
    
}

//POJO
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AllPokemonsResponseTemplateVO {

   private PokemonUrl pokemon;
}

The main goal is to get all the 151 URLs that contain the description from every pokemon and save it in my DB.

Advertisement

Answer

If I understood correctly what you want to achieve, you need to use ParameterizedTypeReference<List<PokemonUrl>>() {} in your restTemplate call so that you can get a list of PokemonUrl as follows:

//Service Class
public ResponseEntity<List<PokemonUrl>> findAllPokemonsApi() {
    log.info("Find All Pokemon From Api Service Call");
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
    
    ResponseEntity<List<PokemonUrl>> response = restTemplate.exchange(GET_ALL_POKEMONS_API, HttpMethod.GET, entity, new ParameterizedTypeReference<List<PokemonUrl>>() {});

    return response;
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement