I'm trying to store every response to a Arrays.asList in Builder but it won't work. I been trying a lot but no luck. How to do it? It's my first time to store a response in a single variable.
Storing response in Arrays.asList in Builder is just a test. Kindly suggest the best way to do it.
//model entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
public class Student{
private String firstname;
private String lastname;
private String age;
}
//model entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class StudentResponse{
private List<String> responsefirstname;
private List<String> responselastname;
private List<String> responseage;
public static StudentResponse convertToResponse(List<Student> student){
return StudentResponse.builder()
.responsefirstname(Arrays.asList(student.get(0).getFirstname))
.responselastname(Arrays.asList(student.get(0).getLastname))
.responseage(Arrays.asList(student.get(0).getAge))
.build();
}
}
//Service
public class StudentService{
@Autowired
private StudentRepo repo;
public StudentResponse findStudent(String sfirstname){
List<Student> students = repo.findByFirstname(sfirstname);
List<StudentResponse> studentResponse = students.stream
// .map(StudentResponse::convertToResponse) //dont work
.collect(Collectors.toList());
return studentResponse
}
}
Expected response must be like this
{
"firstname":["justin","ana","marie"],
"lastname": ["last1","last2","las3"],
"age": [45,55,28]
}
not multiple like this
{
"firstname":"justin",
"lastname": "last2",
"age": 55
},
{
"firstname":"justin",
"lastname": "last1",
"age": 45
}