samu_masken #9

Merged
PBG2H22AWO merged 4 commits from samu_masken into master 2023-12-20 09:56:32 +01:00
3 changed files with 50 additions and 6 deletions
Showing only changes of commit 456e55a66e - Show all commits

View File

@ -5,9 +5,12 @@ import java.util.ArrayList;
public class Kind { public class Kind {
// region Felder // region Felder
private transient int id;
private String name; private String name;
private String vorname; private String vorname;
private int id; private int bid;
// TODO Zutat implementieren! // TODO Zutat implementieren!
// private ArrayList<Zutat> filter; // private ArrayList<Zutat> filter;
// endregion // endregion
@ -37,6 +40,14 @@ public class Kind {
this.id = id; this.id = id;
} }
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
// TODO Zutat implementieren! // TODO Zutat implementieren!
/* /*
public ArrayList<Zutat> getFilter() { public ArrayList<Zutat> getFilter() {
@ -50,10 +61,12 @@ public class Kind {
// endregion // endregion
// region Konstruktoren // region Konstruktoren
public Kind(String name, String vorname) { public Kind(String name, String vorname, int bid) {
this.name = name; this.name = name;
this.vorname = vorname; this.vorname = vorname;
this.bid = bid;
} }
// endregion // endregion
} }

View File

@ -21,15 +21,22 @@ public class RestApiClient implements IRestAPI{
private final HttpClient client; private final HttpClient client;
private final Gson gson;
public RestApiClient(){ public RestApiClient(){
client = HttpClient.newHttpClient(); client = HttpClient.newHttpClient();
gson = new Gson();
} }
public static void main(String[] args){ public static void main(String[] args){
new RestApiClient().get("Kind", 1); Kind kind = new Kind("Klein", "Kevin", 2);
String json = new Gson().toJson(kind);
new RestApiClient().post("Kind", json);
} }
@ -49,7 +56,6 @@ public class RestApiClient implements IRestAPI{
// Send the request and get the response // Send the request and get the response
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()); HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
Gson gson = new Gson();
Kind test = gson.fromJson(httpResponse.body(), Kind.class); Kind test = gson.fromJson(httpResponse.body(), Kind.class);
// Print the response status code and body // Print the response status code and body
@ -77,9 +83,14 @@ public class RestApiClient implements IRestAPI{
// Send the request and get the response // Send the request and get the response
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()); HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
Kind[] test = gson.fromJson(httpResponse.body(), Kind[].class);
// Print the response status code and body // Print the response status code and body
System.out.println("Status Code: " + httpResponse.statusCode()); 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) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -143,7 +154,26 @@ public class RestApiClient implements IRestAPI{
*/ */
@Override @Override
public void post(String controllerName, String jsonData) { 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<String> 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();
}
} }
/** /**

View File

@ -5,6 +5,7 @@ module de.subway_surfers.vpr_app {
requires com.google.gson; 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; exports de.subway_surfers.vpr_app;
} }