在AWS Lambda中使用自定义类时出现JSON解析错误

时间:2016-05-03 03:08:06

标签: java json amazon-web-services aws-lambda

我正在关注AWS Lambda tutorial。我创建了一个Lambda函数,它将自定义类作为输入并输出另一个自定义类。在输入测试数据时,我收到JSON解析错误。

public class HelloPojo implements RequestHandler<RequestClass, ResponseClass>{

@Override
public ResponseClass handleRequest(RequestClass request, Context context){
    String greetingString = String.format("Hello %s, %s.", request.firstName, request.lastName);
    return new ResponseClass(greetingString);
}
}

这是测试输入{ "firstName":"John", "lastName":"Doe" }。我相信AWS需要以json格式输入,然后解析它。以下是自定义requestclass对象:

public class RequestClass {
String firstName;
String lastName;

public RequestClass(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
}
public RequestClass() {
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}

这是错误消息:

 "errorMessage": "An error occurred during JSON parsing",
 "errorType": "java.lang.RuntimeException",
 "stackTrace": [],
 "cause": {
     "errorMessage": "org/json/simple/JSONObject",
     "errorType": "java.lang.NoClassDefFoundError",

这是responseClass:

public class ResponseClass {
String greetings;

public ResponseClass(String greetings) {
    this.greetings = greetings;
}    
public ResponseClass() {
}  
public String getGreetings() {
    return greetings;
}
public void setGreetings(String greetings) {
    this.greetings = greetings;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}

1 个答案:

答案 0 :(得分:3)

如果您看到以json收到的错误消息。你会得到答案。

     "errorMessage": "org/json/simple/JSONObject",
     "errorType": "java.lang.NoClassDefFoundError",
  

错误类型: - NoClassDefFoundError异常意味着,在运行时,依赖jar尝试链接名为org.json.simple.JSONObject的类。它不会在您的library中找到。

解决方案

请将json-simple-1.1.jar添加到lib。如果已存在,则清理工作区并重建项目。它会对你很好。