我正在向我的 OpenWhisk/IBM Cloud 功能添加 CORS 支持。但是在对函数进行修改(-a web-custom-options true)之后,我注意到性能下降了。为了隔离这个问题,我创建了一个简单的函数,见下文:
public static JsonObject main(JsonObject args) throws IOException {
String method = args.get("__ow_method").getAsString();
System.out.println(method+" handle");
if (method.equalsIgnoreCase("OPTIONS")) {
JsonObject responseJSON = new JsonObject();
//add CORS headers
JsonObject headers = new JsonObject();
headers.addProperty("Access-Control-Allow-Headers", "*");
headers.addProperty("Access-Control-Allow-Origin", "https://mjonker.github.io");
headers.addProperty("Access-Control-Allow-Credentials", "true");
responseJSON.add("headers", headers);
responseJSON.addProperty("statusCode", 200);
return responseJSON;
} else {
JsonObject responseJSON = new JsonObject();
JsonObject headers = new JsonObject();
headers.addProperty("Access-Control-Allow-Headers", "*");
headers.addProperty("Access-Control-Allow-Origin", "https://mjonker.github.io");
headers.addProperty("Access-Control-Allow-Credentials", "true");
headers.addProperty("Content-Type", "application/json");
responseJSON.add("headers", headers);
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
JsonObject answerJSON=new JsonObject();
JsonArray timeArray = new JsonArray();
timeArray.add( "It is "+sdf.format(now));
answerJSON.add("text",timeArray);
responseJSON.add("body",answerJSON );
responseJSON.addProperty("statusCode", 200);
return responseJSON;
}
}
有两种情况 1. .http 端点和 web-custom-options true 2. .json 端点和 web-custom-options false
正如您从屏幕截图中看到的,OPTIONS 的差异很大,但 POST 回复的差异也很明显。我可以做些什么来获得 CORS 的一些性能支持吗?我在 JAVA 代码中做错了吗?