I'm using Apache Wink to construct restful services. And analysis() is one of my RESTful services, the code of analysis() is as follows:
public Response analysis(@Context HttpServletRequest req) {
JSONObject conf = new JSONObject();
try{
myProcess();
return Response.ok().build();
} catch(Exception e) {
e.printStackTrace();
JSONObject response = new JSONObject();
response.put(RESTApplication.ERRORCODE, "S001");
response.put(RESTApplication.MESSAGE, "Error occurs");
return Response.serverError().entity(response).type(MediaType.APPLICATION_JSON).build();
}
}
You can see it calls function myProcess(), but the function needs pretty long time to return. So the problem is, Can I return a response message immediately,and return another response when myProcess() finished? And how?
No, you cannot return two responses. However, one way is to issue corresponding GET
request in an asynchronous mode from client side, which can further deal with the response (that can be either success or failure) later when it is available. Meanwhile client (browser) can continue normally.
Using RESTful services provided by apache wink is very easy, all you need to do is setup necessary wink libraries and then connect your server side Java methods to your client side (assuming it is Javascript) via @Path
annotations.
For example, you want to issue such a request at the path your_web_application_context/configuration/check
Then your Java (server side) REST class would be like following
@Path("/configuration")
public class Configuration { // Some class name
@GET
@Path("/check") // configuration/check comes from your request url
public Response analysis(@Context HttpServletRequest req) {
JSONObject conf = new JSONObject();
try{
myProcess();
return Response.ok().build();
} catch(Exception e) {
e.printStackTrace();
JSONObject response = new JSONObject();
response.put(RESTApplication.ERRORCODE, "S001");
response.put(RESTApplication.MESSAGE, "Error occurs");
return Response.serverError().entity(response).type(MediaType.APPLICATION_JSON).build();
}
}
Note: The annotations @GET
, @Path
are provided by wink libraries.
Now having your server side ready, you can issue a request to server side methods via an asynchronous call from client side, which then will process your request and return you a response. For demonstration purposes I am gonna use jQuery's get method for client side.
$.get("<your application context url>/configuration/check",
function(data, status){
// This function is called when the response is available.
// Response data is available in data field.
// Status field tells if the request was success or error occurred.
alert("Data: " + data + "\nStatus: " + status);
}).done(function() {
// This function is called when the response is successfully processed.
// Here you can chain further requests which should only be dispatched
// once the current request is successfully finished
}).fail(function() {
// This function is called when the response is an error.
// Here you can chain further requests which should only be dispatched
// if the current request encounters an error
});
// Statements after $.get function, will continue executing immediately
// after issuing the request. They won't wait for response
// unlike the above "done" and "fail" functions