和实施的Restlet后使用JSON接收响应(Restlet implementing post w

2019-08-03 03:23发布

首先,我想知道什么是我做的是做正确的方式。

我有一种情况,我有会收到一个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);

任何帮助或对这个线索进行HIGHT升值呢?

谢谢,拉胡尔

Answer 1:

当我用下面的JSON作为请求,它的工作原理:

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

注意双引号和额外的冒号是不是你的样本。



Answer 2:

只需使用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 JAR文件- GSON-xyzjarJSE-XYZ / 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);
    }
}


文章来源: Restlet implementing post with json receive and response