'Proper way to change URL address with loop
So I am trying to get the information from a specific API and copy it's data in my database (for test purposes). Here is my code so far:
private static final String GAME_URL = "https://api.rawg.io/api/games";
private static final String API_KEY = "250b2de5e7734f638760ae2bad9bd29f";
private int GAME_ID = 1;
private StringBuilder URL = new StringBuilder(GAME_URL + "/" + GAME_ID + "?key=" + API_KEY);
@Autowired
private RestTemplate restTemplate;
private final GameService gameService;
public FetchGameApiService(GameService gameService) {
this.gameService = gameService;
}
@PostConstruct
public void getGameById(){
for (int i = 1; i <= 100; i++) {
GameDto gameDto = restTemplate.getForObject(URL.toString(), GameDto.class);
if(!checkIfGameValidity(gameDto.getName())){
gameService.saveGame(gameDto);
}
GAME_ID++;
}
}
It seems interesting that for each loop the Game_ID is increasing, however the url always stays the same (instead of api/games/2 or api/games/3 it always stays api/games/1). My question is the following: how can I fix this problem? does it have something to do with Strings? and also is this a proper way to loop though an api and store it in the database?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
