mardi 19 mars 2019

implement sub-resource in REST API

I need to implement sub resource in REST API

URL will be like :
https://localhost:8080/v1/student/{id}?include=address,qualification

so address and qualification are two sub resource , which will be included if we add this is query param .

the issue is happening in implementation side , I have taken @RequestParam List include

so currently i am writing like this in service class

public Student getStudentDetail(Integer id ,List<String> include){
Student student = new Student();
// setting student details
for(String itr: include){
if(itr=="address"){
student.setAddress(repo.getAddress(id));
}
if(itr=="qualification"){
student.setQualication(repo.getQualification(id));
}
}
return student;
}

Student Class:

@Data
public Class Student{
private String id;

private List<Address> address;

private List<Qualification> qualification;

}

so here i need to add if condition for every subresource , can you suggest any better approach to do it or any design principal.

There is another approach to get the repository method name at runtime using reflection API , but it is adding extra overhead to call .

Aucun commentaire:

Enregistrer un commentaire