2009-08-29 15 views
5

सबसे पहले, मैं जानना चाहता था कि मैं क्या कर रहा हूं यह करने का सही तरीका है।जेसन के साथ पोस्ट को पुन: कार्यान्वित करना और प्रतिक्रिया

मेरे पास एक परिदृश्य है जहां मुझे एक जेसन अनुरोध प्राप्त होगा और मुझे डेटाबेस अपडेट करना होगा, एक बार डीबी अपडेट होने के बाद मुझे जेसन स्वीकृति के साथ जवाब देना होगा।

मैं अब तक क्या किया इस प्रकार वर्ग का विस्तार आवेदन पैदा करते हैं:

 @Override 
    public Restlet createRoot() { 
     // Create a router Restlet that routes each call to a 
     // new instance of ScanRequestResource. 
     Router router = new Router(getContext()); 

     // Defines only one route 
     router.attach("/request", RequestResource.class); 

     return router; 
    } 

मेरे संसाधन वर्ग ServerResource प्रदान कर रहा है और मैं अपने संसाधन कक्षा में निम्न विधि है

@Post("json") 
public Representation post() throws ResourceException { 
    try { 
     Representation entity = getRequestEntity(); 
     JsonRepresentation represent = new JsonRepresentation(entity); 
     JSONObject jsonobject = represent.toJsonObject(); 
     JSONObject json = jsonobject.getJSONObject("request"); 

     getResponse().setStatus(Status.SUCCESS_ACCEPTED); 
     StringBuffer sb = new StringBuffer(); 
     ScanRequestAck ack = new ScanRequestAck(); 
     ack.statusURL = "http://localhost:8080/status/2713"; 
     Representation rep = new JsonRepresentation(ack.asJSON()); 

     return rep; 

    } catch (Exception e) { 
     getResponse().setStatus(Status.SERVER_ERROR_INTERNAL); 
    } 

मेरी पहली चिंता यह है कि मैं इकाई में प्राप्त ऑब्जेक्ट इनपुट प्रस्तुति है इसलिए जब मैं jsonobjectation से jsonobject लाता हूं तो मुझे हमेशा खाली/शून्य वस्तु मिलती है।

मैं ग्राहक संलग्न

function submitjson(){ 
alert("Alert 1"); 
    $.ajax({ 
     type: "POST", 
     url: "http://localhost:8080/thoughtclicksWeb/request", 
     contentType: "application/json; charset=utf-8", 
     data: "{request{id:1, request-url:http://thoughtclicks.com/status}}", 
     dataType: "json", 
     success: function(msg){ 
      //alert("testing alert"); 
      alert(msg); 
     } 
    }); 
}; 

ग्राहक निम्नलिखित कोड के साथ json अनुरोध के साथ-साथ

ClientResource requestResource = new ClientResource("http://localhost:8080/thoughtclicksWeb/request"); 
     Representation rep = new JsonRepresentation(new JSONObject(jsonstring)); 
    rep.setMediaType(MediaType.APPLICATION_JSON); 
    Representation reply = requestResource.post(rep); 

कोई मदद या इस पर सुराग कहा करते थे गुजर करने की कोशिश की hight की सराहना की है?

धन्यवाद, राहुल

+0

आधिकारिक Restlet-पर चर्चा मंच पर इस सवाल पूछने पर विचार करें // restl et.tigris.org/ds/viewForumSummary.do?dsForumId=4447 –

उत्तर

1

जब मैं अनुरोध के रूप में निम्नलिखित JSON उपयोग करते हैं, यह काम करता है:

{"request": {"id": "1", "request-url": "http://thoughtclicks.com/status"}} 

सूचना दोहरे उद्धरण चिह्नों और अतिरिक्त पेट है कि आपके नमूना में नहीं हैं।

1

सिर्फ 1 जार का उपयोग JSE-xyz/lib/org.restlet.jar, आप सरल अनुरोधों के लिए ग्राहक के पक्ष में हाथ से JSON का निर्माण कर सकते हैं:

ClientResource res = new ClientResource("http://localhost:9191/something/other"); 

StringRepresentation s = new StringRepresentation("" + 
    "{\n" + 
    "\t\"name\" : \"bank1\"\n" + 
    "}"); 

res.post(s).write(System.out); 

सर्वर साइड पर, बस का उपयोग कर 2 जार - gson-xyzjar और JSE-xyz/lib/org.restlet.jar: http:

public class BankResource extends ServerResource { 
    @Get("json") 
    public String listBanks() { 
     JsonArray banksArray = new JsonArray(); 
     for (String s : names) { 
      banksArray.add(new JsonPrimitive(s)); 
     } 

     JsonObject j = new JsonObject(); 
     j.add("banks", banksArray); 

     return j.toString(); 
    } 

    @Post 
    public Representation createBank(Representation r) throws IOException { 
     String s = r.getText(); 
     JsonObject j = new JsonParser().parse(s).getAsJsonObject(); 
     JsonElement name = j.get("name"); 
     .. (more) .. .. 

     //Send list on creation. 
     return new StringRepresentation(listBanks(), MediaType.TEXT_PLAIN); 
    } 
} 
संबंधित मुद्दे