2014-06-20 51 views
0

我打算實現一個Java客戶端部署和取消部署應用程序到GlassFish,Glassfish的REST API

下面是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(); 

不幸的是,我無法獲得預期的結果。 任何人都可以提供建議嗎?

+0

可怕的問題。什麼是預期的結果,你會得到什麼,而你有什麼嘗試等等等等等等 – thecoshman

回答

1

爲什麼不使用Jersey客戶端。

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

感謝satish,它爲我工作.. :) –

+0

我是新的,如何接受答案? –

+0

點擊左邊的禮品市場反對我的回答。 – Satish