cześć,
chciałem stworzyć aplikację która będzie bazowała na tym, że po uruchomieniu dane będą pobierane z zewnętrznego API ( ranking zawodników UFC podzielony na kategorie wagowe ) i będzie można na nich dopiero operować w formie restowych endpointów.
Zastanawiam się w którym miejscu taki kod umieścić. Wpadłem na pomysł zrobić osobny serwis i klasy do tego. Wszystko fajnie niby testowo działa jednak dostaję NullPointerException przy próbie zapisania danych z Listy używając JpaRepository.
public class FighterDataBaseService {
private LightHeavyweight lightHeavyweight;
private Heavyweight heavyweight;
//itd dalsze kategorie
private FighterRepository fighterRepository;
public FighterDataBaseService() {
lightHeavyweight = new LightHeavyweight();
heavyweight = new Heavyweight();
}
public void runService() throws IOException {
List<Fighter> list = heavyweight.heavyweight();
fighterRepository.saveAll(list); // no tu nie chce zadziałać.
// fighterRepository.saveAll(heavyweight.heavyweight());
}
}
oraz
public class Heavyweight {
private List<Fighter> listHeavyweight;
public Heavyweight() {
listHeavyweight = new ArrayList<>();
}
public List<Fighter> heavyweight() throws IOException {
String url = "https://www.sherdog.com/news/rankings/2/Sherdogs-Official-Mixed-Martial-Arts-Rankings-164999";
Document document = Jsoup.connect(url).get();
Elements fighters = document.select("h2 > a[href]");
for (Element fighter : fighters) {
Fighter fighterToParse = new Fighter();
System.out.println(fighter.text());
String fighterUrl = "https://www.sherdog.com" + fighter.attr("href");
Document doc = Jsoup.connect(fighterUrl).get();
Element fighterData = doc.select("span[class=fn]").first();
fighterToParse.setName(fighterData.text().toString());
fighterData = doc.select("span[class=item birthday]").first();
fighterToParse.setAge(fighterData.text().toString());
fighterData = doc.select("span[class=item weight]").first();
fighterToParse.setWeight(fighterData.text().toString());
fighterData = doc.select("span[class=item height]").first();
fighterToParse.setHeight(fighterData.text().toString());
fighterData = doc.select("strong[class=title]").first();
fighterToParse.setCategory(fighterData.text().toString());
fighterData = doc.select("span[itemprop=name]").first();
fighterToParse.setAssociation(fighterData.text().toString());
listHeavyweight.add(fighterToParse);
// fighterData = doc.select("div.data").first();
// System.out.println(fighterData.text());
System.out.println("---------------");
}
return listHeavyweight;
}
}
Zastanawiam się czy nie jest słabym pomysłem uruchamianie tego w klasie z kontekstem ( bo w sumie tylko raz przy starcie aplikacji mi to jest potrzebne bo później już będę miał w bazie te dane) :
@SpringBootApplication
public class TopMmaApplication {
public static void main(String[] args) throws IOException {
SpringApplication.run(TopMmaApplication.class, args);
FighterDataBaseService test = new FighterDataBaseService();
test.runService();
}
}
czy jest może na to bardziej odpowiednie miejsce + inny rodzaj segregacji tych klas?