In primo luogo, quello che volevo sapere è che quello che sto facendo è il modo giusto per farlo.Post di implementazione del restlet con json receive e response
Ho uno scenario in cui ho ricevuto una richiesta JSON e devo aggiornare il database con quello, una volta aggiornato il db, devo rispondere con il riconoscimento JSON.
quello che ho fatto finora è creare l'applicazione si estende classe come segue:
@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;
}
La mia classe di risorse estende la ServerResource e ho il seguente metodo nella mia classe di risorsa
@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);
}
La mia prima preoccupazione è che l'oggetto che ricevo nell'entità è inputrepresentation, quindi quando prendo jsonobject dalla jsonrepresentation creata ottengo sempre oggetto vuoto/nullo.
ho cercato di passare la richiesta JSON con il seguente codice così come il client collegato
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 utilizzato per chiamare
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);
Qualsiasi aiuto o indizi su questo è alta apprezzato?
Grazie, Rahul
Considerate questa domanda sul Restlet-discutere forum ufficiale: http: // restl et.tigris.org/ds/viewForumSummary.do?dsForumId=4447 –