For more information:
Archives
09-01-10
In a previous blog you learned how to build RESTful web services using Spring, now we are going to show you how build a client to access RESTful web services using Spring's RestTemplate. The RestTemplate was introduce in Spring 3.0 and can be used right out the box by including the org.springframework.web jar in your classpath. The RestTemplate provides entry points for the six main HTTP methods:
The method names indicate the HTTP method that they will invoke and the second part of the name indicates what the method will return. The first argument of each method is the URI. The URI can either be a URI template or a normal URI. I am going to show you how to use the RestTemplate to access the dictionary service that was introduced in a previous blog .
| Http Method | RestTemplate Method |
| DELETE | delete(String, Object...) |
| GET | getForObject(String, Class, Object...) |
| HEAD | headForHeaders(String, Object...) |
| OPTIONS | optionsForAllow(String, Object...) |
| POST | postForLocation(String, Object, Object...) |
| PUT | put(String, Object, Object...) |
The method names indicate the HTTP method that they will invoke and the second part of the name indicates what the method will return. The first argument of each method is the URI. The URI can either be a URI template or a normal URI. I am going to show you how to use the RestTemplate to access the dictionary service that was introduced in a previous blog .
GET
To retrieve all the words out of the dictionary we use the following code:
To retrieve a word:RestTemplate template = new RestTemplate();WordList wordList = template.getForObject(" http://localhost:8080/SpringDictionaryService/bti/dictionary", WordList.class);
Map vars = new HashMap(); vars.put("word", "set");
Word word = template.getForObject(
"http://localhost:8080/SpringDictionaryService/bti/dictionary/{word}",
Word.class, vars);
This method uses a URI template and takes a Map which contains the URI path variables. This same example can be done using a normal URI:
RestTemplate template = new RestTemplate();
Word word = template.getForObject(
"http://localhost:8080/SpringDictionaryService/bti/dictionary/set",
Word.class);
POST
To add a new word to our dictionary we use the following code:
Word newWord = new Word("BTIer", "A great teammate"); URI resourceLocation = template.postForLocation( "http://localhost:8080/SpringDictionaryService/bti/dictionary/", newWord);
The above code will add the word "BTIer" to the dictionary with the definition of "A great teammate". The postForLocation method returns a URI which gives the location of the newly created resource. The URI for this newly added word is "/SpringDictionaryService/bti/dictionary/BTIer".
PUT
To update a word in our dictionary we use the following code:
Word newWord = new Word("BTIer", "The ultimate teammate");
template.put(
"http://localhost:8080/SpringDictionaryService/bti/dictionary/BTIer",
newWord);
DELETE
To remove a word from our dictionary we use the following code:
As you can see Spring provides an easy way to create clients to access RESTful services using the RestTemplate. With the ability to also create RESTful services easily, Spring is a great framework for both implementing and consuming RESTful services.
template.delete("http://localhost:8080/SpringDictionaryService/bti/dictionary/BTIer");
Filed under: Web Services, JAVA, REST, Spring | Comments (0)
08-24-10
Do you work with someone where it would be more beneficial for your team to have your company pay them to stay at home than come to work? If so, you have a
Net Negative Programmer
Net Negative Programmers distract team members with long winded conversations, frequently commit bugs into your repository, ignore team coding standards, etc . . .
Google Tech Talks had a presentation titled "How Open Source Projects Survive Poisonous People." Take a watch. More to come on how to handle Net Negative Programmers.
Net Negative Programmers distract team members with long winded conversations, frequently commit bugs into your repository, ignore team coding standards, etc . . .
Google Tech Talks had a presentation titled "How Open Source Projects Survive Poisonous People." Take a watch. More to come on how to handle Net Negative Programmers.
Filed under: Collaboration, Teamwork | Comments (0)
08-18-10
Since its inception, Ruby on Rails (RoR) has been at the forefront of RESTful Web Service development. And ActiveResource, the RoR answer to the client-side of RESTful web service interaction, is an invaluable tool for integrating your RoR application with the many services that may be available to you.
ActiveResource can take RESTful resources and map them to client-side classes with almost no configuration or code required by the application developer. Lets take a look at the classic “Person” service example:
In these few lines of code we’ve created a Person model class that can be used throughout our application much like our ActiveRecord based model classes that interact with our database. What can we do with this class? Well, out of the box you can do all the standard CRUD operations.
Lets take a look at how some of these operations might look.
So say we want to create a new person. Here is some sample code to do just that:
The save method above will send the following XML to the service in the POST request body:
And any response body from the request will be parsed and assumed to be the saved representation of the new person. Once the person has been saved we can easily make updates:
So we’re following standard convention here to get us moving very quickly. Similarly we could delete our new person:
And now JP is gone.
The final piece to the CRUD puzzle is the ‘find’ method. As with the others, this method behaves similarly to the ActiveRecord find method. Here are some examples:
As you can see ActiveResource provides a lot of power into very little code for the application developer, and this blog has only scratched the surface. ActiveResource has many more options and features to allow you to taylor it to your specific needs and purposes, including validation, error handling and many others.
I would recommend diving into the ActiveResource API documentation on the RoR homepage if you’re interested in learning more about it. Or feel free to shoot me an email, I’d love to discuss it with you.
Happy coding!!!
ActiveResource can take RESTful resources and map them to client-side classes with almost no configuration or code required by the application developer. Lets take a look at the classic “Person” service example:
class Person < ActiveResource::Base
self.site = “http://www.bti360.com/person-service”
end
In these few lines of code we’ve created a Person model class that can be used throughout our application much like our ActiveRecord based model classes that interact with our database. What can we do with this class? Well, out of the box you can do all the standard CRUD operations.
Lets take a look at how some of these operations might look.
So say we want to create a new person. Here is some sample code to do just that:
jp = Person.new(:first_name => 'Jon’, :last_name => ‘Pierce’)
jp.save # sends POST /people/ (create)
The save method above will send the following XML to the service in the POST request body:
<?xml version="1.0" encoding="UTF-8"?>
<person>
<first-name>Jon</first-name>
<last-name>Pierce</last-name>
</person>
And any response body from the request will be parsed and assumed to be the saved representation of the new person. Once the person has been saved we can easily make updates:
jp.new? # => false
jp.last_name = ‘Awesome’
jp.save # sends PUT /people/1 (update)
So we’re following standard convention here to get us moving very quickly. Similarly we could delete our new person:
jp.destroy # sends DELETE /people/1 (delete)
And now JP is gone.
The final piece to the CRUD puzzle is the ‘find’ method. As with the others, this method behaves similarly to the ActiveRecord find method. Here are some examples:
Person.find(1)
# => GET /people/1.xml
Person.find(:all)
# => GET /people.xml
Person.find(:all, :params => { :last_name => "Smith" })
# => GET /people.xml?last_name=Smith
As you can see ActiveResource provides a lot of power into very little code for the application developer, and this blog has only scratched the surface. ActiveResource has many more options and features to allow you to taylor it to your specific needs and purposes, including validation, error handling and many others.
I would recommend diving into the ActiveResource API documentation on the RoR homepage if you’re interested in learning more about it. Or feel free to shoot me an email, I’d love to discuss it with you.
Happy coding!!!
Filed under: Web Services, Ruby on Rails, Resource Oriented, REST, Ruby | Comments (0)



ShareThis