简体   繁体   中英

AWS Lambda Constructor Overloading Java POJOs

I created a Lambda based on this URL: https://docs.aws.amazon.com/lambda/latest/dg/java-handler-io-type-pojo.html . There are three main classes:

package example;

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.firstName, request.lastName);
    return new ResponseClass(greetingString);
}
}

is the Lambda handler.

package example;

 public class RequestClass {
    String firstName;
    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() {
    }
}

is the requestClass.

package example;

 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() {
    }

}

is the response class.

Input is something along the lines of:

{ "firstName": "John", "lastName": "Doe" }  

That being said, I do have some questions regarding the RequestClass. Right now, it is dependent on there being a firstName and lastName provided as part of the input. However, let's say only a firstName is provided, and I want to have another constructor in the RequestClass with just a firstName parameter and initialize the lastName to a default lastName, something like

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

When I try doing something along these lines and access the variables in the handleRequest, I'm able to get the firstName but lastName is always null (guessing because I do not provide it as part of the input). Any reason as to why this happens and what I can do so that when accessing lastName in the handler class, I am able to get "defaultLastName" instead of null?

Please let me know if there are further clarifications I should add, I don't post to StackOverflow very often and want to make sure my question is appropriate!

you could set some default values in your default constructor.

public RequestClass() {
  firstName = "defaultFirstName";
  lastName = "defaultLastName;
}

And then call your parameterized constructor with this .

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

Readings for constructor overloading: https://beginnersbook.com/2013/05/constructor-overloading/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM