Reputation: 13
I plan to implement a java client to deploy and undeploy the application to Glassfish,
Below is the curr command
curl -s -S \
-H 'Accept: application/json' \
-H 'X-Requested-By: dummy' \
-X DELETE http://localhost:4848/management/domain/applications/application/hello
And my java code is
URL url = new URL(
"http://localhost:4851/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
String input = "{\"DELETE\":\"http://localhost:4851/management/domain/applications/application/hello\"}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
Unfortunately, I not able to get the expected result. Anyone can help to advice?
Upvotes: 0
Views: 1287
Reputation: 721
Why don't you use Jersey Client.
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
public class DELETEClient {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:4848/management/domain/applications/application/hello");
String responseData = target.request().header("Accept", "application/json").header("X-Requested-By", "Dummy").delete(String.class);
Response response = target.request().delete();
System.out.println(responseData);
System.out.println(response);
}
}
Upvotes: 1