smalltowndev
smalltowndev

Reputation: 745

Can REST be used to "call" operations on business objects?

I have a new project coming up, and am intruiged by REST. However, it seems to have a very limited interface. Does REST support object-specific interactions, or is it limited to simple CRUD?

Example: A school management app has Student objects. It should be able to:

RegisterNewStudent (some data)

Each student object should handle business operations:

Student.FixName(name data)
Student.ChangeSchool(school data)
Student.Graduate(classrank data)
Student.ChangePassword(password data)

I've been implementing CQRS with message queues where each of these things would be a separate Command. However, in REST it appears I'd be limited to:

PUT Student (all data about student)
POST Student/id (update student record with any/all fields changed)
DELETE Student/id

Am I missing something here? Where would lifecycle/statechange logic be implemented in a RESTful solution? Changing a Student's School enrollment involves different logic (and possibly different permissions) than changing her name. Would I have to let the client post an "update" of any/all fields and then have to infer what operations they intend?

//Edit:

Is this the sort of thing I'm struggling toward:

PUT /Students {data about new student}
POST /Students/314/School {data about different school}
POST /Students/314/Name {data to fix name, ie add middle name}
POST /Students/314/Password {data for new password}

etc?

Upvotes: 1

Views: 657

Answers (2)

James Black
James Black

Reputation: 41858

You will want to create several webservices to do what you want, but if I take one example of yours:

Student.ChangePassword(password data)

And have a POST request /ChangePassword/{student_id}/{password}

then your only trick is to ensure who can call this function. Do you use a session cookie to control access, or have them pass in a username/password then a new password?

You will find that POST and PUT will be useful as these aren't logged in the webserver log file.

Upvotes: 0

paulsm4
paulsm4

Reputation: 121799

Q: Can REST be used to “call” operations on business objects?

A: Yes, absolutely yes.

Just as you can invoke any operation on any servlet with GET or PUT, you can also invoke any operation in a REST-ful web service.

Including, but not limited to, CRUD operations :)

Upvotes: 2

Related Questions