如何使用 apache wink 来 return 异步响应?
how can I return asynchronous response by using apache wink?
我正在使用 Apache Wink 构建 restful 服务。而analysis()是我的RESTful服务之一,analysis()的代码如下:
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();
}
}
您可以看到它调用了函数 myProcess(),但是该函数需要相当长的时间才能 return。所以问题是,我可以立即 return 一个响应消息,并在 myProcess() 完成后 return 另一个响应吗?又如何?
不,您不能 return 两个回复。但是,一种方法是从客户端以异步模式发出相应的GET
请求,这可以在以后进一步处理响应(可以是成功或失败)它是可用的。同时客户端(浏览器)可以正常继续。
使用 apache wink 提供的 RESTful 服务非常简单,您需要做的就是设置必要的 wink 库,然后将您的服务器端 Java 方法连接到您的客户端(假设它是Java脚本)通过 @Path
注释。
比如你想在路径your_web_application_context/configuration/check
发出这样的请求
那么你的 Java(服务器端)REST class 就像下面的
@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();
}
}
注:注释@GET
、@Path
由wink库提供。
现在您的服务器端已准备就绪,您可以通过来自客户端的异步调用向服务器端方法发出请求,然后客户端将处理您的请求并return 你一个回应。出于演示目的,我将在客户端使用 jQuery's get 方法。
$.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
我正在使用 Apache Wink 构建 restful 服务。而analysis()是我的RESTful服务之一,analysis()的代码如下:
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();
}
}
您可以看到它调用了函数 myProcess(),但是该函数需要相当长的时间才能 return。所以问题是,我可以立即 return 一个响应消息,并在 myProcess() 完成后 return 另一个响应吗?又如何?
不,您不能 return 两个回复。但是,一种方法是从客户端以异步模式发出相应的GET
请求,这可以在以后进一步处理响应(可以是成功或失败)它是可用的。同时客户端(浏览器)可以正常继续。
使用 apache wink 提供的 RESTful 服务非常简单,您需要做的就是设置必要的 wink 库,然后将您的服务器端 Java 方法连接到您的客户端(假设它是Java脚本)通过 @Path
注释。
比如你想在路径your_web_application_context/configuration/check
发出这样的请求那么你的 Java(服务器端)REST class 就像下面的
@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();
}
}
注:注释@GET
、@Path
由wink库提供。
现在您的服务器端已准备就绪,您可以通过来自客户端的异步调用向服务器端方法发出请求,然后客户端将处理您的请求并return 你一个回应。出于演示目的,我将在客户端使用 jQuery's get 方法。
$.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