Erstens, was ich wissen wollte ist, was ich tue, ist der richtige Weg, es zu tun.Restlet Implementierung Post mit JSON erhalten und Antwort
Ich habe ein Szenario, wo ich eine JSON-Anfrage erhalten habe und ich muss die Datenbank damit aktualisieren, sobald die DB aktualisiert wird, muss ich mit der JSON-Bestätigung antworten.
Was ich bisher getan haben ist es, die Klasse erweitern Anwendung zu erstellen, wie folgt:
@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;
}
Meine Ressourcenklasse ist die ServerResource erstreckt, und ich habe die folgende Methode in meiner Ressourcenklasse
@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);
}
Meine erste Sorge ist, dass das Objekt, das ich in der Entität empfange, inputrepresentation ist, also, wenn ich das jsonobject von der jsonrepresentation erhalte, die ich erschaffe, erhält ich immer leeres/Nullgegenstand.
Ich habe versucht,
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 verwendet, angebracht sowie die Client die json Anfrage mit dem folgenden Code Weitergabe dieses
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);
Hilfe oder Hinweise zu nennen ist Höhe geschätzt?
Danke, Rahul
Betrachten Sie diese Frage auf der offiziellen Restlet-Diskussion Forum fragen: http: // restl et.tigris.org/ds/viewForumSummary.do?dsForumId=4447 –