Beispiel:
Ruft alle Produkte ab
http://localhost:8080/api/v2/products/allproducts
API-Ablauf:
- Ruft alle Produkte ab;
Erstellt eine neue temporäre_Datei; - Schreibt in eine neue temporäre_Datei;
- Speichert Informationen in der Datei und gibt sie zurück;
Löscht temp_file;
Wenn ich die API auf Heroku bereitstelle, erhalte ich einen 500 Internal Server Error „java.io.FileNotFoundException“ (kein solcher). Datei oder Verzeichnis) – der Catch-Block des Aufrufs
(Weitere Informationen finden Sie weiter unten)
dyno=web.1 connect=0ms service=4ms status=500 bytes=871 protocol=https 2023-03-20T22:04:36.792371+00:00 app[web.1]: Unable to create tempfile 2023-03-20T22:04:36.792387+00:00 app[web.1]: java.io.FileNotFoundException: ./src/main/java/org/test/resources/temp_allproducts.json (No such file or directory) 2023-03-20T22:04:36.792523+00:00 app[web.1]: at java.io.FileOutputStream.open0(Native Method) 2023-03-20T22:04:36.792524+00:00 app[web.1]: at java.io.FileOutputStream.open(FileOutputStream.java:270) 2023-03-20T22:04:36.792533+00:00 app[web.1]: at java.io.FileOutputStream.(FileOutputStream.java:213) 2023-03-20T22:04:36.792534+00:00 app[web.1]: at java.io.FileOutputStream.(FileOutputStream.java:162) (...)
Ich frage mich, ob der Dateipfad in Heroku anders ist oder ob es einfach etwas gibt, das ich im beispielhaften Ablauf nicht mache.
Ich' Ich hinterlasse Ihnen ein Beispiel dafür, wie ich diese temporäre Datei erstelle.
private static File makeTempFile(String data, String fileName) {
File file = new File(BASE_PATH + "temp_" + fileName+ ".json");
try (FileWriter writer = new FileWriter(file)) {
writer.write(formatJson(data));
System.out.println(BASE_PATH + "temp_" + fileName + ".json" + " created");
return file;
} catch (Exception e) {
System.out.println("Unable to create tempfile");
e.printStackTrace();
return null;
}
}