2013-04-29 9 views
21

के साथ डेटा सबमिट करने के लिए मैं जर्सी के लिए नया हूं। POST विधि के साथ डेटा जमा करने के लिए मुझे जर्सी क्लाइंट को लागू करने की आवश्यकता है। कर्ल आदेश है:जर्सी क्लाइंट 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 

तो मैं कैसे ऊपर कर्ल आदेश लागू करने के लिए जर्सी क्लाइंट का उपयोग करने पता लगाने की कोशिश कर रहा हूँ।

अब तक मैंने किया है:

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 पोस्ट विधि प्यार के आधार पर। हैश मैप। साफ। – jettero

+0

यह अच्छा लगता है, लेकिन मैं इसे FEATURE_POJO_MAPPING और jersey-json.jar: क्लाइंटहैंडलर अपवाद: जावा प्रकार, क्लास java.util.HashMap, और MIME मीडिया प्रकार, एप्लिकेशन/जेसन के लिए एक संदेश बॉडी लेखक के साथ काम करने के लिए नहीं मिल सकता है, नहीं मिला - मुझे क्या याद आ रही है? – TheArchitect

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

+0

इसके अलावा, मुझे लगता है कि 2.x के लिए यह प्रतिक्रिया होना चाहिए, क्लाइंट रिस्पॉन्स नहीं। – Erhannis

+0

@ एरहनीस आप दोनों पर सही हैं, मैंने जवाब तय किया है। धन्यवाद –

संबंधित मुद्दे