Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Proper REST API design - resources in the controllers

 
Ranch Hand
Posts: 109
1
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good evening! I'm finally building a REST application and could use some help regarding the API design, or rather lack there of.

What I have so far works and makes sense for me, but I doubt it's a "good" design and should ask for proper help anyway. At least start now before adding error handling etc

class Person : person entity holding info
class Measurement : @ManyToOne to Person, a person can have many measurements, in the person's profile i can see all their measurements
class Appointment: @OneToOne to Person, a person can have (or not) an appointment

Measurement and Appointment are the owner entities and it's a unidirectional association

Requirements:
I want to see all the persons in the database, then navigate to each profile
In the person's profile I can see their measurements, I can add or remove measurements
In the person's profile I can see if they have an appointment, if yes navigate to it
I want to see all appointments in a page, regardless of persons
Each appointment will show the person's info and nothing more

PersonController:
HTTPEndpointMethod
GET/personsfindAll
PST/personscreatePerson
GET/persons/{id}findByID
DEL/persons/{id}deletePerson

MeasurementController:
HTTPEndpointMethod
GET/persons/{id}/measurementsfindMeasurements
PST/persons/{id}/measurementsaddMeasurement
DEL/persons/{id}/measurements/{mid}removeMeasurement

AppointmentController:
HTTPEndpointMethod
GET/appointmentsfindAllAppointments
GET/appointments/{id}findAppointmentByID
DEL/appointments/{id}removeAppointment
GET/appointments/todaygetTodaysAppointments
GET/appointments/tomorrowgetTomorrowsAppointments
GET/persons/{pid}/appointmentfindByPersonID
PST/persons/{pid}/appointmentcreateAppointment


With the above "design", the UI in my mind works. I've read though that all resources should be under the same path, and here I have /persons endpoints in other controllers. Should this be redesigned or it's OK according to the requirements? Perhaps move the /persons/{id}/appointment endpoints in the PersonController and just leave the /appointments/... endpoint in the AppointmentController?
 
Saloon Keeper
Posts: 13174
285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try and keep resources with the same route prefix in the same controller. That means you should put /persons/{pid}/appointment under PersonController. Depending on the MVC framework you're using, there might be a third option: use subresource controllers:

As a final remark, I strongly urge you to use different path prefixes if a general and a specific path would otherwise collide. For instance, use appointmentsByDay/tomorrow and appointmentsById/{id} to avoid weird situations where the string "tomorrow" could be interpreted as an ID.
 
Vasilis Souvatzis
Ranch Hand
Posts: 109
1
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fantastic! This is great help thank you very much! I've read about REST design but it's much more understandable with my own code

OK so I'll have two final Controllers, one serving the /persons path and one serving the /appointmentsByDay. Much better organized!

I've known about subresources but wasn't sure how they worked. Your outline is invaluable! Current project is in Spring Boot 2.0 and it definitely has subresources.

In fact I think I'll refactor straight to subresources because (if I'm not mistaken) I'm more or less reusing the same controllers I have now.

Thanks for the hands-on help
 
Stephan van Hulst
Saloon Keeper
Posts: 13174
285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome, I'm glad it helped. :-)
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic