diff --git a/pom.xml b/pom.xml index 8505bb5..9e4eaf5 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,11 @@ ${junit.version} test + + com.google.code.gson + gson + 2.10.1 + diff --git a/src/main/java/Logik/Kind.java b/src/main/java/Logik/Kind.java index 17d9bcd..191ca90 100644 --- a/src/main/java/Logik/Kind.java +++ b/src/main/java/Logik/Kind.java @@ -5,9 +5,12 @@ import java.util.ArrayList; public class Kind { // region Felder + + private transient int id; private String name; private String vorname; - private int id; + private int bid; + // TODO Zutat implementieren! private ArrayList filter; // endregion @@ -37,6 +40,14 @@ public class Kind { this.id = id; } + public int getBid() { + return bid; + } + + public void setBid(int bid) { + this.bid = bid; + } + // TODO Zutat implementieren! public ArrayList getFilter() { @@ -49,10 +60,12 @@ public class Kind { // endregion // region Konstruktoren - public Kind(String name, String vorname) { + public Kind(String name, String vorname, int bid) { this.name = name; this.vorname = vorname; + this.bid = bid; } + // endregion diff --git a/src/main/java/RestAPISchnittstelle/RestApiClient.java b/src/main/java/RestAPISchnittstelle/RestApiClient.java index 1805d87..6827f5a 100644 --- a/src/main/java/RestAPISchnittstelle/RestApiClient.java +++ b/src/main/java/RestAPISchnittstelle/RestApiClient.java @@ -1,9 +1,3 @@ -/** - * @author Samuel Wolff - * noch nicht getestet - * TODO FERTIG MACHEN - */ - package RestAPISchnittstelle; import java.net.URI; @@ -11,22 +5,38 @@ import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; +import java.nio.file.WatchEvent; +import Logik.Kind; +import com.google.gson.Gson; + +/** + * noch nicht getestet + * TODO FERTIG MACHEN + * @author Samuel Wolff + */ public class RestApiClient implements IRestAPI{ private final String urlBase = "https://pbg2h22awo.web.pb.bib.de/VPR_Schnittstelle/VPR_Schnittstelle/restAPI.php"; private final HttpClient client; + private final Gson gson; + public RestApiClient(){ client = HttpClient.newHttpClient(); + gson = new Gson(); } public static void main(String[] args){ - new RestApiClient().get("Kind", 2); + Kind kind = new Kind("Klein", "Kevin", 2); + + String json = new Gson().toJson(kind); + + new RestApiClient().post("Kind", json); } @@ -46,9 +56,11 @@ public class RestApiClient implements IRestAPI{ // Send the request and get the response HttpResponse httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()); + Kind test = gson.fromJson(httpResponse.body(), Kind.class); + // Print the response status code and body System.out.println("Status Code: " + httpResponse.statusCode()); - System.out.println("Response Body: " + httpResponse.body()); + System.out.println("Response Body: " + test.getName()); } catch (Exception e) { e.printStackTrace(); } @@ -71,9 +83,14 @@ public class RestApiClient implements IRestAPI{ // Send the request and get the response HttpResponse httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()); + Kind[] test = gson.fromJson(httpResponse.body(), Kind[].class); + // Print the response status code and body System.out.println("Status Code: " + httpResponse.statusCode()); - System.out.println("Response Body: " + httpResponse.body()); + for(Kind i : test){ + System.out.println(i.getVorname()); + } + //System.out.println("Response Body: " + test); } catch (Exception e) { e.printStackTrace(); } @@ -137,7 +154,26 @@ public class RestApiClient implements IRestAPI{ */ @Override public void post(String controllerName, String jsonData) { + URI apiUri = URI.create(String.format("%s/%s", urlBase,controllerName)); + System.out.println(apiUri); + + HttpRequest httpRequest = HttpRequest.newBuilder() + .uri(apiUri) + .header("Content-Type", "application/json") + .POST(HttpRequest.BodyPublishers.ofString(jsonData, StandardCharsets.UTF_8)) + .build(); + + try { + // Send the request and get the response + HttpResponse httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()); + + // Print the response status code and body + System.out.println("Status Code: " + httpResponse.statusCode()); + System.out.println("Response Body: " + httpResponse.body()); + } catch (Exception e) { + e.printStackTrace(); + } } /** diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 336a917..dfa6c61 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -2,8 +2,10 @@ module de.subway_surfers.vpr_app { requires javafx.controls; requires javafx.fxml; requires java.net.http; + requires com.google.gson; - opens de.subway_surfers.vpr_app to javafx.fxml; + opens de.subway_surfers.vpr_app to javafx.fxml, com.google.gson; + opens Logik to com.google.gson; exports de.subway_surfers.vpr_app; } \ No newline at end of file