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/
Related
I have a users database in firebase that contains user's first name and last name
So, i want to take only the head name (i.e) Stephen, Steve, Tony e.t.c.
And i want to show them as a list in a recycler view.
Is this possible?
This is my java object
public class Users {
String firstName, lastName;
public Users(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = 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;
}
}
and i have created my adapter that extends RecyclerView.Adapter<>
PS: The names are not always same as the first name.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Beginner here.
I cant get my head around why this code outputs the default halfway through. Can anyone take look?
sorry if the format is wrong, first time posting and will fix if not correct.
public class officemanager {
public static void main(String[] args) {
Staffmember aStaffMember = new Staffmember("Steven", "bob");
System.out.println(aStaffMember.toString());
Programmer appleprg = new Programmer("Marion", "bob", "Java");
appleprg.getLanguage();
System.out.println(appleprg.toString());
Doctor dr = new Doctor();
dr.setWard(5);
dr.setFirstName("ed");
dr.setLastName("fall");
System.out.println(dr.toString());
}
}
OUTPUT
Staffmember firstName=Steven, lastName=bob
Programmer firstName=Marion , lastName=bob language Java
default constructor
Doctor firstName=ed , lastName=fall Ward 5
Sorry guys here the class the default constructor is in. It is the Superclass called Staffmember and the firstname, lastname Strings are passed through it.
package oopinheritance;
public class Staffmember {
private String firstName;
private String lastName;
// default constructor
public Staffmember() {
System.out.println("default constructor");
}
// constructor
public Staffmember(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = 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 String toString() {
return "Staffmember firstName="
+firstName+ ", lastName=" + lastName;
}
}
Here is the Doctor class, it is a subclass of Staffmember and it has its own tostring method:
package oopinheritance;
public class Doctor extends Staffmember{
private int ward;
public int getWard() {
return ward;
}
public void setWard(int ward) {
this.ward = ward;
}
public String toString() {
return "Doctor firstName="
+this.getFirstName() + " , lastName=" + this.getLastName() + " \t
ward" + this.ward;
}
}
As you have not shown your whole program, so its hard to tell where is the error, but it might be in the default constructor of the doctor class.
Anyways here is the code that you can refer. It will give the correct output.
Here is the link you can refer to see the execution order
http://javabeginnerstutorial.com/learn-by-example-3/order-of-execution-of-blocks-in-java/
Java Constructors - Order of execution in an inheritance hierarchy
class GfG {
public static void main(String[] args) {
Staffmember aStaffMember = new Staffmember("Steven", "bob");
System.out.println(aStaffMember.toString());
Programmer appleprg = new Programmer("Marion", "bob", "Java");
appleprg.getLanguage();
System.out.println(appleprg.toString());
Doctor dr = new Doctor();
dr.setWard(5);
dr.setFirstName("ed");
dr.setLastName("fall");
System.out.println(dr.toString());
}
}
class Staffmember {
String firstName;
String lastname;
public Staffmember(String firstName, String lastname) {
super();
this.firstName = firstName;
this.lastname = lastname;
}
#Override
public String toString() {
return "Staff Member firstName=" + firstName + ", lastname=" + lastname;
}
}
class Programmer {
String firstName;
String lastName;
String Language;
public String getLanguage() {
return Language;
}
public void setLanguage(String language) {
Language = language;
}
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 Programmer(String firstName, String lastname, String Language) {
super();
this.firstName = firstName;
this.lastName = lastname;
this.Language = Language;
}
#Override
public String toString() {
return "Programmer firstName=" + firstName + ", lastName=" + lastName + ", Language=" + Language;
}
}
class Doctor {
int ward;
String firstName;
String lastName;
public void setWard(int ward) {
this.ward = ward;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastname) {
this.lastName = lastname;
}
public Doctor(int ward, String firstName, String lastName) {
super();
this.ward = ward;
this.firstName = firstName;
this.lastName = lastName;
}
#Override
public String toString() {
return "Doctor ward=" + ward + ", firstName=" + firstName + ", lastName=" + lastName;
}
}
I am trying to run a lambda function which will get couple of nested JSON's as input. I am trying to map them with POJO classes. But the nested JSON's values are returning null.
I have given a sample of the problem here.
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();
}
}
Try renaming class parameters to class Parameters. With uppercase.
There is some issue with getter and setter functions of your code. I tried running your code in eclipse and faced same error. I think getter and setter functions of your parameter class is improper. [Agree with Iam, rename parameter class to Parameter, and update getter, setter methods accordingly]
Also, AWS Lambda does the deserialization of input JSON objetcs for you, so you may not need to use Jackson Annotation for deserializing JSON input.
Thanks!
I have create a custom JavaBean I want to return from my MBean method. The following is the custom JavaBean:
package org.text.jmx;
public class Person {
private firstName;
private lastName;
public Person(){
}
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
The MBean interface is the following:
package org.text.jmx;
public interface TestJmxMBean {
public Person getPerson();
public void setPerson(String firstName, String lastName);
}
The class the implements the MBean:
package org.text.jmx;
public class TestJmx implements TestJmxMBean {
private Person person = new Person();
public Person getPerson() {
return person;
}
public void setPerson(String firstName, String lastName) {
person.setFirstName(firstName);
person.setLastName(lastName);
}
}
I create a server application that registers the above MBean, which is successful. I create a client application which successfully connects to the server application via JMX, but when I call the testJmx.getPerson() method from the client application is receive an error that it can't return the Person object. What am I doing wrong? It works fine is I just define the return type as as String or String[] from the TestJmx.getPerson().
In order to expose a custom object as a JMX attribute, or the return value or an operation, it must be defined as an OpenType. The usual way of doing this is to define an MXBean. I answered a similar question which should give you an idea on how to proceed.
Actually i am implementing one program in which i am retrieving information from database and i am setting that info to One Object using setters and i just want to print that Object parameters without using system.out.println(object.getHeadCount());
I just want like if i am giving system.out.println(object); so using this code it should print data in Json format or any other readable format.
How to do it.Because my object is containing 30 fields so it is very hectic to write 30 getters to print data.
You have to override toString()
Normal way
class User {
private String firstName;
private String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
#Override
public String toString(){
return this.firstName + " " + this.lastName;
}
}
Using Apache Commons
class User {
private String firstName;
private String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
#Override
public String toString(){
return new ToStringBuilder(this)
.append("firstName", firstName)
.append("lastName", lastName)
.toString();
}
}
Using Google Guava
class User {
private String firstName;
private String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
#Override
public String toString(){
return MoreObjects.toStringHelper(this)
.add("firstName", firstName)
.add("lastName", lastName)
.toString();
}
}
You simply have to override the toString() method of the class, read about it in java doc for Object class. Most IDE support it's automatic generation for all the fields of your class or for a number of them. Just for example:
class User {
private String name;
private String surname;
User(String name, String surname)
{
this.name = name;
this.surname = surname;
}
#Override
public String toString()
{
return this.name+" "+this.surname;
}
}
You should override toString() method. if you are using eclipse then you can try right-click within the editor, you'll find it under Source -> Generate toString()
To make a toString method you can simply just add one like so.
public String toString(){
return "" + getValue();
}
The toString method is a part of
java.lang.Object
So it is implemented in every class.