映射到AWS Lambda中的POJO的嵌套JSON返回null

时间:2017-05-31 07:02:16

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

我正在尝试运行一个lambda函数,它将获得几个嵌套的JSON作为输入。我试图用POJO类映射它们。但嵌套的JSON值返回null。 我在这里给出了一个问题的样本。

    Input:
{
  "firstName": "Raj",
  "lastName": "Guru",

  "parameters" : {
      "Address ": "Testaddress",
      "POBOX" : "123"
  }
}

OutPut:
Uploading function code to lambda...
Upload success. Function ARN: arn:aws:lambda:eu-central-1:938487755516:function:lambda
Invoking function...
==================== FUNCTION OUTPUT ====================
{"greetings":"Hello `Raj,` null.null"}

package com.amazonaws.lambda.demo;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class HelloPojo implements RequestHandler<RequestClass, ResponseClass> {

public ResponseClass handleRequest(RequestClass request, Context context){
    String greetingString = String.format("Hello %s, %s.", request.getFirstName(), request.getparameters().getAddress());
 greetingString+= request.getparameters().getAddress();

            return new ResponseClass(greetingString);
}

---------------------------------------------------------------------------------------------------------------
package com.amazonaws.lambda.demo;

public class parameters {
    private  String Address;
    private  String POBOX;

    public String getAddress() {
        return Address;
    }
    public void setAddress(String Address) {
        this.Address = Address;
    }
    public String getPOBOX() {
        return POBOX;
    }
    public void setPOBOX(String POBOX) {
        this.POBOX = POBOX;
    }
    public parameters(String Address,String POBOX) {

        this.Address = Address;
        this.POBOX = POBOX;
    }
     public parameters() {
        }
    @Override
    public String toString(){
        return getPOBOX()+getAddress();
    }



}

// RequestClass:

package com.amazonaws.lambda.demo;

import com.fasterxml.jackson.annotation.JsonProperty;

public class RequestClass {

    private parameters parameters = new parameters( );

    @JsonProperty("parameters") 
    public parameters getparameters() {
        return parameters;
    }


     public void setparameters(parameters paramters) {

        this.parameters = paramters;
    }



        package com.amazonaws.lambda.demo;

 public class ResponseClass {
    String greetings;

    public String getGreetings() {
        return greetings;
    }

    public void setGreetings(String greetings) {
        this.greetings = greetings;
    }

    public ResponseClass(String greetings) {
        this.greetings = greetings;
    }

    public ResponseClass() {
    }

}



      private   String firstName;
          private  String lastName;

            public String getFirstName() {
                return firstName;
            }

            public void setFirstName(String firstName) {
                this.firstName = firstName;
            }

            public String getLastName() {
                return lastName;
            }

            public void setLastName(String lastName) {
                this.lastName = lastName;
            }

            public RequestClass(String firstName, String lastName) {
                this.firstName = firstName;
                this.lastName = lastName;
            }

            public RequestClass() {
            }
    @Override
            public String toString(){
                StringBuilder sb = new StringBuilder();
                sb.append("firstName=" + getFirstName() + "\n");
                sb.append("lastName=" + getLastName() + "\n");
                sb.append("parameters =" + getparameters() + "\n");
                return sb.toString();
            }
        }

2 个答案:

答案 0 :(得分:0)

尝试将类参数重命名为类Parameters。用大写字母。

答案 1 :(得分:0)

代码的getter和setter函数存在一些问题。我尝试在eclipse中运行你的代码并遇到同样的错误。我认为你的参数类的getter和setter函数是不合适的。 [同意Iam,将参数类重命名为Parameter,并相应地更新getter,setter方法]

此外,AWS Lambda会为您执行输入JSON对象的反序列化,因此您可能不需要使用Jackson Annotation来反序列化JSON输入。

谢谢!