GSON works
This commit is contained in:
@@ -21,15 +21,22 @@ public class RestApiClient implements IRestAPI{
|
||||
|
||||
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", 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
|
||||
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
Gson gson = new Gson();
|
||||
Kind test = gson.fromJson(httpResponse.body(), Kind.class);
|
||||
|
||||
// Print the response status code and body
|
||||
@@ -77,9 +83,14 @@ public class RestApiClient implements IRestAPI{
|
||||
// Send the request and get the response
|
||||
HttpResponse<String> 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();
|
||||
}
|
||||
@@ -143,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<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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user