Đầu tiên, điều tôi muốn biết là những gì tôi đang làm là đúng cách để thực hiện.Khôi phục lại việc thực hiện bài đăng với json nhận và trả lời
Tôi có một kịch bản mà tôi sẽ nhận được yêu cầu json và tôi phải cập nhật cơ sở dữ liệu với điều đó, sau khi cập nhật db tôi phải trả lời lại với sự thừa nhận json.
gì tôi đã làm cho đến nay là tạo ra các ứng dụng mở rộng lớp như sau:
@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;
}
lớp tài nguyên của tôi là mở rộng ServerResource và tôi có phương pháp sau đây trong lớp tài nguyên của tôi
@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);
}
Mối quan tâm đầu tiên của tôi là đối tượng tôi nhận được trong thực thể là inputrepresentation vì vậy khi tôi lấy jsonobject từ jsonrepresentation, tôi luôn nhận được đối tượng rỗng/null.
Tôi đã thử đi qua các yêu cầu json với đoạn mã sau cũng như các khách hàng gắn liền
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);
}
});
};
Khách hàng sử dụng để gọi
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);
Bất kỳ sự giúp đỡ hoặc manh mối về vấn đề này được đánh giá cao Hight?
Cảm ơn, Rahul
Xem xét hỏi câu hỏi này trên Restlet-thảo luận diễn đàn chính thức: http: // restl et.tigris.org/ds/viewForumSummary.do?dsForumId=4447 –