RESTful URLs in Spring
by, 11-28-2011 at 12:31 AM (1775 Views)
In my previous tip, I presented to you an overview of Spring support of REST. In this tip, I will followup with a tip for look at RESTful URLs.
If you want to create a RESTful controller, you will need to handle URLs in a RESTful way. Thatís because everything thatís done in REST is done through a URL. The URLís RESTful nature is even implied by itís name, uniform resource locator. Given that name, it seems that a URL is intended to locate a resource. Above and beyond this, URLs are also URIs, or uniform resource identifiers.
What RESTful URLs do is to acknowledge that HTTP is about resources. In this case, the URL doesnít do anything. It only identifies the resource. Itís the HTTP methods that decide what is done with the resource. The URL doesnít even have query parameters but instead use them to provide guidance to the server in producing the resource. The final thing that is important to know is that RESTful URLs in general are hierarchical. That means as you go left to right they tend to go from the general to the more precise. Below are examples of RESTful URLs:
http://localhost:8080 identifies a domain and port. Although our application wonít associate a resource with this URL, thereís no reason why it couldnít.
http://localhost:8080/Springexample identifies the applicationís servlet context. This URL is more specific in that it has identified an application running on the server.
http://localhost:8080/Springexample/car identifies a resource that represents a list of Car objects within the Springexample application.
http://localhost:8080/Springexample/car/fordfiesta2011 is the most precise URL, identifying a specific Car resource.
The interesting thing about RESTful URLs is that its path is parameterized. The RESTful URLís input is part of the URLís path whereas the RESTless whereas the RESTless URL takes its input from query parameters,. To be able to handle requests for that kind of URL, weíll need a way to write a controllerís handler method so that it can take input from the URLís path. This will be the topic of the next tip. See you then!