Olá, tenho uma dúvida. Aqui optei por fazer 1 método só dispararRequisicao e passei como parametro algumas coisas que eram necessárias para utilizar o método ficou mais ou menos assim:
private static HttpResponse<String> dispararRequisicao(String param, String method, String endPoint, JsonObject json) throws IOException, InterruptedException {
if (!param.isBlank() && method.equals("GET")) {
HttpClient client = HttpClient.newHttpClient();
String uri = API_URL + param + endPoint;
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(uri))
.method(method, HttpRequest.BodyPublishers.noBody())
.build();
return client.send(request, HttpResponse.BodyHandlers.ofString());
} else if (!param.isBlank() && method.equals("POST")) {
HttpClient client = HttpClient.newHttpClient();
String uri = "http://localhost:8080/abrigos/" + param + endPoint;
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(uri))
.header("Content-Type", "application/json")
.method(method, HttpRequest.BodyPublishers.ofString(json.toString()))
.build();
return client.send(request, HttpResponse.BodyHandlers.ofString());
} else if (method.equals("POST")){
HttpClient client = HttpClient.newHttpClient();
String uri = "http://localhost:8080/abrigos";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(uri))
.header("Content-Type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString(json.toString()))
.build();
return client.send(request, HttpResponse.BodyHandlers.ofString());
} else {
HttpClient client = HttpClient.newHttpClient();
String uri = "http://localhost:8080/abrigos";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(uri))
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
return client.send(request, HttpResponse.BodyHandlers.ofString());
}
}
Eu testei e funcionou, não sei se é certo deixar um método para fazer tudo (get,post,patch...etc)