我一直在尝试部署一个 lambda 函数,然后通过 API 网关使其可访问。我的 java 函数在 JAVA 中,这是我在 JAVA 中创建一个简单的 AWS lambda 函数所遵循的文档:
https://docs.aws.amazon.com/lambda/latest/dg/java-handler-io-type-pojo.html
这是我的函数处理程序的外观:
package lambda;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import lambda.axon.Path;
public class shortestPath implements RequestHandler<RequestClass, ResponseClass>{
public ResponseClass handleRequest(RequestClass request, Context context){
String inputString = request.inputString;
int steps = Path.stepsTo(inputString);
return new ResponseClass(steps);
}
}
这是这个请求类:
package lambda;
public class RequestClass {
String inputString;
public String getInputString() {
return inputString;
}
public void setInputString(String inputString) {
this.inputString = inputString;
}
public RequestClass(String inputString) {
this.inputString = inputString;
}
public RequestClass() {
}
}
这是响应类:
package lambda;
public class ResponseClass {
int steps;
public ResponseClass(int steps) {
this.steps = steps;
}
public ResponseClass() {
}
public int getSteps() {
return steps;
}
public void setSteps(int steps) {
this.steps = steps;
}
}
我在aws上部署它并配置AWS api网关来触发它,当我使用邮递员时,当我到达API网关给出的端点时一切正常(https://www.getpostman.com/)
但是通过浏览器尝试相同的操作会给我一个 CORS 错误:
Access to XMLHttpRequest at 'https://<hash>execute-api.us-east-1.amazonaws.com/dev' from origin 'http://localhost:4200' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.
我尝试在 API 网关控制台中启用 CORS,然后重新部署它:
这篇 Stackoverflow 帖子(在 AWS Lambda 上配置 CORS 响应标头?)说如果我使用 lambda-proxy,我应该在处理程序响应本身中包含标头,我不确定代理是什么,但我如何用我当前的Lambda 函数的实现,即在我的响应中包含自定义标头
largeQ
慕哥9229398
相关分类