简体   繁体   中英

Glassfish REST API

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?

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);
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM