我试图用来自Java Lambda的POST请求解析'Body'。 我暂时坚持这个错误。
org.json.simple.JSONObject cannot be cast to java.lang.String
但是记录下的Body看起来像那样:
{"body":{"email":"test@test.com"}}
女巫应该解析我做得对吗? 奇怪的是,插件在JUNIT本地工作但在AWS之后不在线。
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
logger = context.getLogger();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
Number user_id = null;
String birthdate = null;
List<Number> company_id = new ArrayList<Number>();
String email = null;
String employment_status = null;
String firstname = null;
String lastname = null;
String login = null;
String profile = null;
List<Number> site_id = new ArrayList<Number>();
String validation_status = null;
JSONObject responseJson = new JSONObject();
Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
String filterExpression = "";
String RegionAWS = REGION.toString();
client = AmazonDynamoDBClientBuilder.standard().withRegion(RegionAWS).build();
DynamoDB dynamoDB = new DynamoDB(client);
Table table = dynamoDB.getTable("LI_user");
try {
JSONParser parser = new JSONParser();
JSONObject event = (JSONObject) parser.parse(reader);
logger.log(event.toJSONString());
if (event.get("body") != null) {
JSONObject bod = (JSONObject)parser.parse((String)event.get("body"));
// JSONObject bod = (JSONObject) event.get("body");
if ( bod.get("id") != null) {
user_id = (Number)bod.get("id");
}
if ( bod.get("birthdate") != null) {
birthdate = (String)bod.get("birthdate");
}
if ( bod.get("email") != null) {
email = (String) bod.get("email");
}
if ( bod.get("employment_status") != null) {
employment_status = (String) bod.get("employment_status");
}
if ( bod.get("firstname") != null) {
firstname = (String) bod.get("firstname");
}
if ( bod.get("lastname") != null) {
lastname = (String) bod.get("lastname");
}
if ( bod.get("login") != null) {
login = (String) bod.get("login");
}
if ( bod.get("profile") != null) {
profile = (String) bod.get("profile");
}
if ( bod.get("validation_status") != null) {
validation_status = (String) bod.get("validation_status");
}
}
答案 0 :(得分:1)
替换
JSONObject bod = (JSONObject)parser.parse((String)event.get("body"));
带
JSONObject bod = (JSONObject)event.get("body");
如果event
始终是JSONObject
的实例(因为它似乎被授予,否则您将获得JSONObject event = (JSONObject) parser.parse(reader);
的ClassCast
您看到已记录
{"body":{"email":"test@test.com"}}
仅仅因为logger.log(event.toJSONString());
答案 1 :(得分:0)
谢谢你,我有进步。 但现在看来,当我用Postman发送JSON格式的数据时,他收到了正文的字符串版本。我在cloudwatch日志中获得了这些数据:
"resource": "/users",
"httpMethod": "POST",
"queryStringParameters": null,
"stageVariables": null,
"body": "{\n \"id\": 5,\n \"company_id\": [\n 1\n ],\n \"email\": \"test@test.com\",\n \"employment_status\": \"FULL-TIME\",\n \"firstname\": \"Sebastien\",\n \"lastname\": \"LALALLALA\",\n \"login\": \"test@test.com\",\n \"profile\": \"FULL-TIME\",\n \"site_id\": \"55\",\n \"birthdate\": \"1985.05.05\",\n \"validation_status\" : \"COMPLETE\"\n}\n"
}
这个错误:
java.lang.ClassCastException: java.lang.String cannot be cast to org.json.simple.JSONObject