繁体   English   中英

Glassfish REST API

[英]Glassfish REST API

我计划实现一个Java客户端,以将应用程序部署和取消部署到Glassfish,

以下是curr命令

curl -s -S \
    -H 'Accept: application/json' \
    -H 'X-Requested-By: dummy' \
    -X DELETE http://localhost:4848/management/domain/applications/application/hello

我的java代码是

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

不幸的是,我无法获得预期的结果。 任何人都可以提供建议吗?

您为什么不使用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);
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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