2013-04-29 57 views
21

我是澤西島的新手。我需要實現一個Jersey客戶端來使用POST方法提交數據。 curl命令是:如何使用Jersey客戶端POST方法提交數據

curl -d '{"switch": "00:00:00:00:00:00:00:01", "name":"flow-mod-1", "priority":"32768", "ingress-port":"1","active":"true", "actions":"output=2"}' http://localhost:8080/wm/staticflowentrypusher/json 

所以我試圖找出如何使用Jersey客戶端來實現上述curl命令。

到目前爲止,我已經做了:

public class FLClient { 

private static Client client; 
private static WebResource webResource; 
private static String baseuri = "http://localhost:8080/wm/staticflowentrypusher/json"; 
private static ClientResponse response; 
private static String output = null; 

public static void main(String[] args) { 
    try { 

     client = Client.create(); 

     webResource = client.resource(baseuri); 

        // implement POST data 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

} 

有人可以幫助我呢?

回答

27

現在我明白了。這裏是我的解決方案:

public static void main(String[] args) { 

    try { 
     Client client = Client.create(); 

     WebResource webResource = client.resource(baseuri); 

     String input = "{\"switch\": \"00:00:00:00:00:00:00:01\", " 
       + "\"name\":\"flow-mod-1\", \"priority\":\"32768\", " 
       + "\"ingress-port\":\"1\",\"active\":\"true\", " 
       + "\"actions\":\"output=2\"}"; 

     // POST method 
     ClientResponse response = webResource.accept("application/json") 
       .type("application/json").post(ClientResponse.class, input); 

     // check response status code 
     if (response.getStatus() != 200) { 
      throw new RuntimeException("Failed : HTTP error code : " 
        + response.getStatus()); 
     } 

     // display response 
     String output = response.getEntity(String.class); 
     System.out.println("Output from Server .... "); 
     System.out.println(output + "\n"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 
18

如果你想發佈一個JSON體內,這是一個更好的方法。

ClientConfig clientConfig = new DefaultClientConfig();    
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);  
client = Client.create(clientConfig); 

WebResource webResource = client.resource(baseuri); 

Map<String,Object> postBody = new HashMap<String,Object>(); 
//put switch, name,priority.... 
ClientResponse response = webResource.accept("application/json") 
       .type("application/json").post(ClientResponse.class, postBody); 

記住,你必須包括jersey-json

+1

我愛這個JSON-少JSON POST方法。 HashMap中。整齊。 – jettero 2013-11-12 15:10:13

+0

這聽起來很酷,但我無法讓它與FEATURE_POJO_MAPPING和jersey-json.jar配合使用: ClientHandlerException:Java類型,類java.util.HashMap和MIME媒體類型application/json的消息主體編寫器,沒有找到 - 我錯過了什麼? – TheArchitect 2014-03-25 01:04:40

6

對於未來的用戶,與jersey事物的新版本已經改變,所以做這樣的POST方法的方式是:

  • 適用版本1.x@Li'answer):

    WebResource webResource = client.resource(baseuri); 
    
    String input = "..."; 
    
    ClientResponse response = webResource.accept("application/json") 
         .type("application/json").post(ClientResponse.class, input); 
    
  • 對於2.x版本:

    WebTarget webTarget = client.target(baseuri); 
    
    String input = "..."; 
    
    Response response = webTarget.request("application/json").post(Entity.json(input)); 
    

基礎上Migration Guide

+0

我不認爲(WebTarget).accept()是2.x的一部分,至少現在不是。 – Erhannis 2016-12-19 20:16:56

+0

另外,我認爲對於2.x應該是Response,而不是ClientResponse。 – Erhannis 2016-12-19 20:28:19

+0

@Erhannis你說的都對,我確定了答案。謝謝 – 2016-12-19 21:06:00

相關問題