2009-08-29 8 views
5

먼저, 내가하고 싶은 것은 올바른 방법입니다.json으로 포스트를 구현하는 Restlet 수신 및 응답

나는 json 요청을 받고 데이터베이스를 업데이트해야하는데, 일단 db가 업데이트되면 json 승인으로 응답해야합니다.

내가 이제까지는 다음과 같이 클래스 확장 응용 프로그램을 만들 것입니다 무엇을 :

 @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); 
    } 

내 첫 번째 관심사는 엔터티에서받는 개체가 inputrepresentation인데 jsonrepresentation에서 jsonobject를 가져 오면 항상 빈/null 개체가 만들어집니다.

나는 높이가 감사

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

어떤 도움이나 이에 대한 단서를 호출하는 데 사용되는 다음 코드로 JSON 요청뿐만 아니라

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

클라이언트 연결 클라이언트를 통과 시도?

감사합니다, 라훌

+0

공식으로 Restlet - 토론 포럼에이 질문을 고려 // restl et.tigris.org/ds/viewForumSummary.do?dsForumId=4447 –

답변

1

내가 요청에 따라 다음 JSON을 사용 , 그것은 작동 : 샘플에없는

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

주의 따옴표 및 추가 콜론.

1

단지 1 JAR를 사용 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); 

서버 측에서 이 JAR 파일 - GSON-xyzjarJSE-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); 
    } 
} 
관련 문제