gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:02:24 25 4
gpt4 key购买 nike

我正在关注 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",

这是响应类:

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;
}

最佳答案

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

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

Error Type :- NoClassDefFoundError Exception means, At runtime when a dependent jar try to link the class named org.json.simple.JSONObject. It will not found in your library.

解决方案

请将 json-simple-1.1.jar 添加到库中。如果已经存在,那么清理你的工作区并重建你的项目。它对你很好用。

关于java - 在 AWS Lambda 中使用自定义类时出现 JSON 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36994940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com