简体   繁体   中英

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,login

so address,qualification and login are three sub resource , which will be included if we add this is query param .Subresource can be database call or rest call to other service

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));
    }
    if(itr=="login"){
    student.setLogin(client.getLogin(id));// here client in Rest Call for 
    }


   }
    return student;
    }

Student Class:

@Data
public Class Student{
private String id;

private List<Address> address;

private List<Qualification> qualification;

private Login login; 
}

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 .

Another Approach can be:

I can use strategy Design pattern

Abstract Class 

public Abstract class Subresource{
public Student getSubresouce(Integer id,Student student){}
}

public class Address extends Subresource{
@Autowired
Databaserepo repo;

public Student getSubresource(Integer id , Student student){
  return student.setAddress(repo.getAddress(id));
}
}

public class Login extends Subresource{
@Autowired
RestClient client;

public Student getSubresource(Integer id, Student student){
 return student.setLogin(client.getLogin(id));
}
}

but in this approach i am unable to write logic in service

  public Student getStudentDetail(Integer id ,List<String> include){
        Student student = new Student();
        // setting student details
        for(String itr: include){
       // Need to fill logic
// Help me here to define logic to use strategy oattern
       }
        return student;
        }

What you are looking for is Projections. From this link

Projections are a way for a client to request only specific fields from an object instead of the entire object. Using projections when you only need a few fields from an object is a good way to self-document your code, reduce payload of responses, and even allow the server to relax an otherwise time consuming computation or IO operation.

If you are using Spring here are some examples.

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