Restlet使用json接收和响应实现post

Restlet使用json接收和响应实现post,json,postgresql,rest,restlet,Json,Postgresql,Rest,Restlet,首先,我想知道的是我正在做的是正确的方法 我有一个场景,我将收到一个json请求,我必须用它更新数据库,一旦数据库被更新,我必须用json确认回复 到目前为止,我所做的是创建类扩展应用程序,如下所示: @Override public Restlet createRoot() { // Create a router Restlet that routes each call to a // new instance of Sc

首先,我想知道的是我正在做的是正确的方法

我有一个场景,我将收到一个json请求,我必须用它更新数据库,一旦数据库被更新,我必须用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时,我总是得到空/空对象

我已经尝试用以下代码和附加的客户端传递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);
        }
  });
};
客户过去常打电话来

    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);
非常感谢您提供的任何帮助或线索

谢谢,
Rahul

当我使用以下JSON作为请求时,它可以工作:

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

请注意示例中没有的双引号和附加冒号。

仅使用1个JARjse-x.y.z/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-x.y.z.jar和jse-x.y.z/lib/org.restlet.jar

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

在官方REST讨论论坛上问这个问题: