Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the command pattern compatible with RESTful web services?

Google recommends the use of the command pattern when building complex GWT applications. The idea seems to have some merit and various frameworks have been developed to help. However, most of the examples I found are using GWT-RPC for client/server communication.

We are considering developing a RESTful web service for our Java backend -- and using that for client/server communication (possibly using a framework like Resty).

But is the command pattern even compatible with Rest web services? What would those Rest URLs look like? How would command batching and undo be supported?

like image 987
HDave Avatar asked Nov 17 '11 14:11

HDave


People also ask

Which option is not a RESTful API constraint?

The only optional constraint of REST architecture is code on demand. If a service violates any other constraint, it cannot strictly be referred to as RESTful.

What is Command pattern in Java?

A Command pattern is an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. (A sender is an object that invokes an operation, and a receiver is an object that receives the request to execute a certain operation.

What are RESTful web services?

RESTful Web Services are basically REST Architecture based Web Services. In REST Architecture everything is a resource. RESTful web services are light weight, highly scalable and maintainable and are very commonly used to create APIs for web-based applications.

What is difference between REST API and RESTful API?

Put simply, there are no differences between REST and RESTful as far as APIs are concerned. REST is the set of constraints. RESTful refers to an API adhering to those constraints. It can be used in web services, applications, and software.


2 Answers

The command pattern is/was a recommended pattern and is a massive step forward from building an RPC interface using the RemoteService and RemoteServiceServlet which is pretty much all that was available at the time of the Google I/O presentation you refer to.

The GWT-RPC approach is very good and works well and provides the batching and undo mechanism. I have to say I've never implemented a single undo() function on any of my handlers although I do make use of batching.

A newer approach would be to use RequestFactory and map your code onto your service. This does support batching but not undo. It's not a great deal of work to implement but does have its peculiarities. A nice example can be found here.

If you need a REST interface then making use of this in your GWT to save developing two interfaces seems like a sensible idea. But as @Riley Lark says you'll have to write your own batching (and undo() if you need to).

My feeling is that both batching and undo don't really fit with the REST approach, but that's just my opinion. There's some more info here from a similar question.

Perosnally I would probably make use of GWTs infrastructure and RequestFactory to do client to server comms and take advantage of the batching and the optomised protocol and the cross site scripting protection and write a separate REST interface for whatever needs to use it.

like image 92
pillingworth Avatar answered Nov 02 '22 04:11

pillingworth


You would have to implement batching yourselves if you don't use some package that supports it. At our company we decided to go with a straight REST interface - the expense of writing our own batching over a single API was less than the expense of making a RequestFactory API and a public API.

You'd have to somehow encode your multiple rest urls and payloads into a single url and payload!

like image 36
Riley Lark Avatar answered Nov 02 '22 05:11

Riley Lark