Restlet用JSON接收和响应实现发布 [英] Restlet implementing post with json receive and response

查看:127
本文介绍了Restlet用JSON接收和响应实现发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想知道的是我在做什么,这是正确的方法.

First, what i wanted to know is what i am doing is the right way to do it.

我有一个场景,我将收到一个json请求,并且我必须用它来更新数据库,一旦数据库更新,我就必须以json确认来回复.

I have a scenario where i have will receive a json request and i have to update the database with that, once the db is updated i have to respond back with the json acknowledgment.

到目前为止,我所做的就是创建如下的类扩展应用程序:

What i have done so far is create the class extending application as follows:

     @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,我的资源类中具有以下方法

My resource class is extending the ServerResource and i have the following method in my resource class

@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时,我总是会得到空/空对象.

My first concern is the object i receive in the entity is inputrepresentation so when i fetch the jsonobject from the jsonrepresentation created i always get empty/null object.

我尝试通过以下代码以及附加的客户端传递json请求

I have tried passing the json request with the following code as well as the client attached

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

曾经打电话给客户的客户

Client used to call

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

对此有任何帮助或线索是值得赞赏的吗?

Any help or clues on this is hight appreciated ?

谢谢, 拉胡尔

推荐答案

仅使用1个JAR jse-xyz/lib/org.restlet.jar ,您可以在客户端手动构建JSON对于简单的请求:

Using just 1 JAR jse-x.y.z/lib/org.restlet.jar, you could construct JSON by hand at the client side for simple requests:

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-x.y.z.jar jse-x.y.z/lib/org.restlet.jar :

At the server side, using just 2 JARs - gson-x.y.z.jar and 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);
    }
}

这篇关于Restlet用JSON接收和响应实现发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆