5 postgresql rest json restlet
首先,我想知道的是我正在做的是正确的方法.
我有一个场景,我将收到一个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;
}
Run Code Online (Sandbox Code Playgroud)
我的资源类是扩展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);
}
Run Code Online (Sandbox Code Playgroud)
我的第一个问题是我在实体中收到的对象是输入表示,所以当我从创建的json代表获取jsonobject时,我总是得到空/ null对象.
我尝试使用以下代码以及附加的客户端传递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);
}
});
};
Run Code Online (Sandbox Code Playgroud)
客户过去常常打电话
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);
Run Code Online (Sandbox Code Playgroud)
任何有关这方面的帮助或线索都会受到高度赞赏吗?
谢谢,拉胡尔
当我使用以下 JSON 作为请求时,它可以工作:
{"request": {"id": "1", "request-url": "http://thoughtclicks.com/status"}}
Run Code Online (Sandbox Code Playgroud)
请注意示例中不存在的双引号和附加冒号。
| 归档时间: |
|
| 查看次数: |
16326 次 |
| 最近记录: |