Lombok @delegate annotation not passing values to innerDTO

383 views Asked by At

inside my test class

String json="{\n" +
                "\t\"masterName\": \"test1\",\n" +
                "\t\"masterSubjectName\": \"testsubject\",\n" +
                "\t\"masterRoll\": \"534\",\n" +
                "\t\"firstName\": \"studentFirstName\",\n" +
                "\t\"rollNumber\": \"23\"\n" +
                "}";
        
        Student studentDTO=new Gson().fromJson(json, Student.class);
        System.out.println(studentDTO);

Student.java

@Data
public class Student {
    

    @Delegate(types = Master.class)
    private Master master=new Master();

    private String firstName;
    private String lastName;
    private int rollNumber;
}

Master.java

@Data
public class Master {

    private String masterName;
    private String masterSubjectName;
    private int masterRoll;
}

This gives Response:

Student(master=Master(masterName=null, masterSubjectName=null, masterRoll=0), firstName=studentFirstName, lastName=null, rollNumber=23)

When I parse the json string "json" to the Student class,

  1. Why values not getting passed to "Master.java" inner dto?

  2. I need something like Student(masterName=test1, masterSubjectName=testsubject, masterRoll=534, firstName=studentFirstName,rollNumber=23)

1

There are 1 answers

0
notAPPP On

Your 'types' value in @Delegate should be Interface with getter methods for example getMasterName() , getMasterSubjectName... and so on

documentation: https://projectlombok.org/features/experimental/Delegate