Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Learning About RESTful Web Services

DZone's Guide to

Learning About RESTful Web Services

Until a few weeks ago, I figured RESTful web service was a very big concept that couldn't be learned easily without understanding the whole architecture.

· Integration Zone
Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90 day trial 3Scale by Red Hat

I remember a few years back when I knew nothing about AJAX. The way people talked about it used to make me nervous. I used to think it had to be something difficult to learn. However, when I actually started doing AJAX myself, I found it to be much easier than what I anticipated.

The same thing happened to me with RESTful web services. I used to think RESTful web service must be a very big concept and something that can't be learned easily without understanding the whole architecture, until a few weeks back when I got the responsibility of teaching this to a group of students. Of course, that means I had to learn it by myself in the first place.

As anticipated, during my few days of extensive research, I found REST architecture itself to be a very big concept and even now I am not totally clear about the concept. Neither are other leading people in this field. I found lots of articles claiming that the way we use RESTful web services in our web applications does not completely follow REST architecture as described by Roy Fielding. People don't care because the way RESTful web services are implemented is already doing good enough job to achieve all the Software Engineering benefits expected for an API.

So in my findings, the implementation aspect is limited to separating data (formatted as JSON or XML) from the representation (HTML in the context of web). By just doing this, we are no longer bound to only browsers to render HTML. We can serve any client as long as they know how to interpret data formats like JSON or XML. It's up to the client to decide what to do with the received data. Of course, there are principles and guidelines to do this in a proper way, for which I found Dr. Dobb's article to be a good reference.

OK, the above paragraph involved what to do. How to do it is even more easier. In my case, I was doing RESTful web services using Spring Web MVC. Creating web services was as easy as adding two more annotations (namely @ResponseBody and @RequestBody) in your existing controller classes. As stated above, it's better to follow the guidelines and come up with separate controller classes specific to serving rest requests, but to make it work, it's as easy as adding few more annotations.

Here's a code snippet from my controller class:

@RequestMapping(value="rest/cart" method = RequestMethod.POST)

    public @ResponseBody

    Cart create(@RequestBody Cart cart) {

        return cartService.create(cart);

    }

And that's it! Now, because of @RequestBody, Spring MVC knows how to convert cart representation (JSON or XML) coming from the client to a Java object, and because of @ResponseBody, it knows how to convert the returned cart (Java Object) back to JSON or XML.

I am sure it should be something of similar complexity in other frameworks and/or languages for implementing RESTful web services. So, stop fearing and start daring.

Discover how you can achielve enterpriese agility with microservices and API management

Topics:
apis ,restful web services ,integration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}