0

I have the following entity:

  @Data
  @Entity
  public class DailyEntry {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;

  private LocalDate date;
  private LocalTime startTime;
  private LocalTime endTime;
  private Duration breaks;
  private String performanceRecord;
  private EntryStatus status;

  @ManyToOne
  private Project project;

  @ManyToOne
  private Employee employee;
}

In the RepositoryRestResource i have the following method defined:

@PostFilter("hasRole('ROLE_BACKOFFICE') or hasRole('ROLE_ADMIN')")
List<DailyEntry> findByProjectId(@Param("id") Long id);

Im able to call that API-method with the following URL for example:

http://localhost:8080/api/dailyEntries/search/findByProjectId?id=1005

This will return all dailyEntries whose project has the id 1005.

Thats how i call it on the client site:

 projects.forEach(project => {
          const httpParams = new HttpParams().set('id', project.id.toString());
          const dailyEntriesObservable = this.dailyEntryService.search('findByProjectId', httpParams); 
          // the definition of the search method is not important
   });

The problem is that in some cases i have around a hundret(and more) projects which resulsts in a hundret(and more) requests which slows things down. Instead of doing a request for every single project, i would like to do one single request for all projects. Should i just send the Ids of the project in the RequestBody? How would the URL look like? Would it be something like http://localhost:8080/api/dailyEntries/search/findByProjectIds with the Ids being in the RequestBody? Ive never seen such a GET-request, so im not sure how the standarts here are for the REST-URL-Design or whether im doing something wrong and theres actually a better way to do it.

M.Dietz
  • 710
  • 5
  • 13
  • Check this https://stackoverflow.com/questions/2602043/rest-api-best-practice-how-to-accept-list-of-parameter-values-as-input – surendrapanday Nov 14 '19 at 12:17
  • @surendrapanday so i can achieve what i want by sending multpile ids inside the URL as `RequestParams` like shown here `http://our.api.com/Product?id=101404&id=7267261`. Thats what i thought about doing at first, but, isnt the URLS length limitation around 2000 characters? `id=101404&id=7267261` already has around 20 characters, if i do this for all of my 100 projects, i would already be at around 1000 characters. So i would be limited to sending around max 200 projects in the request. And having an URL that long sounds weird, but i guess thats the way to go. – M.Dietz Nov 14 '19 at 12:26
  • You can try sending list of ids as the request body. https://stackoverflow.com/questions/34789357/how-to-pass-liststring-in-post-method-using-spring-mvc – surendrapanday Nov 14 '19 at 12:28
  • Ive read here https://stackoverflow.com/questions/978061/http-get-with-request-body that its not recommended to send data via the request body when doing a GET-request. But the answers are 10 years old, so i dont know whether something changed. – M.Dietz Nov 14 '19 at 12:31
  • I could use a POST-request to send a body, but using POST for a getter method seems to be bad design. But i cant think of any other way of doing it which allows me to send an unlimited amount of data. – M.Dietz Nov 14 '19 at 12:54

0 Answers0