I am trying to build database - and i want to get data that i need from API which have an authentication token - im using Java, Spring, MySql DB
I've created simple example of what i want to achieve - in this example i DONT need additional header with authentication token - and my question is can i get data this way when i need to add one:
@Component
public class DataLoader implements CommandLineRunner {
private final ObjectMapper mapper;
private final CountryRepository repository;
public DataLoader(ObjectMapper mapper, CountryRepository repository) {
this.mapper = mapper;
this.repository = repository;
}
@Override
public void run(String... args) throws Exception {
List<Country> countries = new ArrayList<>();
JsonNode json;
try {
json = mapper.readValue(new URL("https://api.football-data.org/v4/areas"), JsonNode.class);
} catch (IOException e) {
throw new RuntimeException("Failed to read JSON data", e);
}
JsonNode areas = getAreas(json);
for (JsonNode area : areas) {
countries.add(createCountryFromNode(area));
}
repository.saveAll(countries);
}
private Country createCountryFromNode(JsonNode area) {
String code = area.get("countryCode").asText();
String name = area.get("name").asText();
return new Country(code, name);
}
private JsonNode getAreas(JsonNode json) {
return Optional.ofNullable(json)
.map(j -> j.get("areas"))
.orElseThrow(() -> new IllegalArgumentException("Invalid JSON Object"));
}
}
So, in example above, when i use readValue() method, everything works fine, i get JSON, and i can work with it - BUT, when i want to use for example:
"https://api.football-data.org/v4/competitions/PL"
instead of:
"https://api.football-data.org/v4/areas"
my response will look like this:
"message": "The resource you are looking for is restricted and apparently not within your permissions. Please check your subscription.",
"errorCode": 403
Ive created account, i got my personal token, i got all permissions now, but i have to add this token as HTTP header.
FINALLY:
Can I add header with key token to an URL in mapper.readValue() method?
Is there any way to parse JSON by Jackson with api that have a key token? Spring annotations, anything? Or maybe i have to use "raw" Java classes (HttpRequest, HttpResponse etc.)?