Home > database >  RestTemplate parsing from Response entity to an Object
RestTemplate parsing from Response entity to an Object

Time:11-13

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.

CodePudding user response:

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

CodePudding user response:

As per the share API response of getAllPokemon, the Java POJO which should resemble the structure of response and capture should be like below:

public class PokemonUrl{


private int count;
private String next;
private Object previous;
private List<PokemonData> results;

// setter, getters


}

public class PokemonData{

private String name;
private String url;

// setter, getters

}

Cannot find what is the datatype of field previous because it's value is null in the shared response, Hence declared the previous field as Object class to bind its value irrespective of it's datatype.

  • Related