Amazon web services 使用Lambda+测试API端点;API网关
我正在尝试使用AWS Lambda和API网关创建和测试API端点。我可以使用Lambda test成功测试我的函数,但当我尝试测试我的端点时,它会给出:Amazon web services 使用Lambda+测试API端点;API网关,amazon-web-services,testing,aws-lambda,aws-api-gateway,Amazon Web Services,Testing,Aws Lambda,Aws Api Gateway,我正在尝试使用AWS Lambda和API网关创建和测试API端点。我可以使用Lambda test成功测试我的函数,但当我尝试测试我的端点时,它会给出: { “消息”:“内部服务器错误” } 这是我的处理程序类: package com.amazonaws.lambda.gandhi.conversion.api; import java.io.IOException; import java.security.NoSuchAlgorithmException; import java.se
{
“消息”:“内部服务器错误”
}
这是我的处理程序类:
package com.amazonaws.lambda.gandhi.conversion.api;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.RandomStringUtils;
import com.amazonaws.lambda.gandhi.conversion.api.Response.AuthClientCredentialResponse;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.lambda.gandhi.conversion.api.utils.ClientAuthPOJO;
public class AuthClientCredentialServiceHandler implements RequestHandler<ClientAuthPOJO, Object> {
private AuthClientCredentialResponse authClientCredentialResponse;
private static final SecureRandom RANDOM = new SecureRandom();
public static int MAX_CLIENT_KEY = 10;
public static int CLIENT_SECRET_LENGTH = 69;
@Override
public AuthClientCredentialResponse handleRequest(ClientAuthPOJO clientIdSecret, Context context) {
String clientSecret;
try {
context.getLogger().log("Input: "
+ clientIdSecret);
String clientId = clientIdSecret.getClientId();
clientSecret = generateClientSecretKey();
Map<String, String> clientCredsMap = getClientCredentials();
if (clientCredsMap.size() > MAX_CLIENT_KEY) {
throw new RuntimeException(String.format("Max limit is %d, Please delete some keys", MAX_CLIENT_KEY));
}
clientCredsMap.forEach((k, v) -> {
if (clientId.equals(k)) {
throw new RuntimeException("Client Already exists");
}
});
storeClientCredentials(clientId, clientSecret);
AuthClientCredentialResponse authClientCredentialResponse = AuthClientCredentialResponse.builder().success(
true).clientId(clientId).clientSecret(clientSecret).build();
this.authClientCredentialResponse = authClientCredentialResponse;
} catch (Exception e) {
throw new RuntimeException(
"Failed to generate client secret: "
+ e.getMessage());
}
return authClientCredentialResponse;
}
private String generateClientSecretKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
String clientSecret = RandomStringUtils.randomAlphanumeric(CLIENT_SECRET_LENGTH);
System.out.printf("clientSecret: %s%n", clientSecret);
return clientSecret;
}
private void storeClientCredentials(String clientId, String clientSecret) throws IOException {
/*
* TODO:
* Some logic to store clientCredentials to a file or DB. Decide later.
*/
System.out.println("temp ClientCredentials stored");
}
public Map<String, String> getClientCredentials() throws IOException {
/*
* TODO:
* Some logic to fetch clientCredentials from file or DB. Decide later.
*/
Map<String, String> clientCredMap = new HashMap<String, String>();
clientCredMap.put("1", "secretKey1");
clientCredMap.put("2", "secretKey2");
clientCredMap.put("3", "secretKey3");
clientCredMap.put("4", "secretKey4");
return clientCredMap;
}
}
我在lambda中的测试对象:
API网关中端点的我的测试:
有人能帮我找出创建函数或API网关的问题吗
编辑:
当我检查日志时,我发现函数(clientId和clientSecret)的参数为null。因此,我发送请求正文的方式似乎存在一些问题。lambda日志说明了什么日志说明:未能生成客户端密码:null:java.lang.RuntimeException java.lang.RuntimeException:未能生成客户端密码:null atcom.amazonaws.lambda.gandhi.conversion.api.AuthClientCredentialServiceHandler.HandlerRequest(AuthClientCredentialServiceHandler.java:48)位于java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法)…当我看到日志时,我发现clientId和clientSecret为null。因此,我发送请求正文的方式似乎存在一些问题。您是否可以记录传入的事件?我认为传入的请求被字符串化了。您需要进行json解析,我不知道如何在java中进行解析lambda日志说什么日志说:未能生成客户机机密:null:java.lang.RuntimeException java.lang.RuntimeException:未能生成客户机机密:null位于com.amazonaws.lambda.gandhi.conversion.api.AuthClientCredentialServiceHandler.HandlerRequest(AuthClientCredentialServiceHandler.java:48)位于java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法)..当我看到日志时,我发现clientId和clientSecret为空。因此,我发送请求正文的方式似乎存在一些问题。您是否可以记录传入事件?我认为传入的请求是字符串化的。您需要进行json解析,我不确定在java中如何进行
package com.amazonaws.lambda.gandhi.conversion.api.utils;
public class ClientAuthPOJO {
String clientId;
String clientSecret;
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getClientSecret() {
return clientSecret;
}
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}
public ClientAuthPOJO(String clientId, String clientSecret) {
super();
this.clientId = clientId;
this.clientSecret = clientSecret;
}
public ClientAuthPOJO() {
}
}