• HOME
    • ABOUT
      • Who We Are
      • 360° of Collaboration
      • Core Values
      • Team BTI
      • Corporate Giving
    • CLIENTS
      • Intelligence Community
    • SERVICES
      • Services Overview
      • Web Services Expertise
      • Hot Deploy
      • Collaborative Software Practices
      • Key Technologies
      • Frameworks and Tools
      • BTI Blog
    • JOIN BTI
      • Introduction
      • Ultimate Teammate
      • Collaborative Growth Cycle
      • Team Fun Squad
      • Job Opportunities
      • Benefits
      • Apply Now
    • CONTACT
      • Login
      • |
      • Register

      For more information:

      • Send an email

      Archives

      • September 10 (1)
      • August 10 (3)
      • July 10 (1)
      • June 10 (2)
      • May 10 (6)
      • April 10 (8)
      • March 10 (6)
      • February 10 (1)
      • January 10 (2)
      • October 09 (3)
      • September 09 (3)
      • August 09 (2)
      • June 09 (2)
      • April 09 (1)

      Categories

      • Web Services (23)
      • Collaboration (11)
      • Kanban (5)
      • Ruby on Rails (6)
      • JAVA (6)
      • Teamwork (8)
      • Communication (5)
      • Agile (2)
      • Resource Oriented (5)
      • Hyper-Text Driven (1)
      • REST (22)
      • Media-Types (2)
      • Testing (1)
      • JEE (3)
      • Ruby (3)
      • Spring (6)
      • Video (5)
      Site Feed
      View all posts for this blog
      09-01-10
      RESTful clients with Spring's RestTemplate
      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:

       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:

      RestTemplate template = new RestTemplate();
      WordList wordList = template.getForObject("
          http://localhost:8080/SpringDictionaryService/bti/dictionary", 
          WordList.class);
      
      
      To retrieve a word:

      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:

      template.delete("http://localhost:8080/SpringDictionaryService/bti/dictionary/BTIer");
      
      


      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.
      ShareThis
      Filed under: Web Services, JAVA, REST, Spring | Comments (0)
      08-24-10
      Net Negative Programmers
      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.

      ShareThis
      Filed under: Collaboration, Teamwork | Comments (0)
      08-18-10
      Smooth sailing with ActiveResource
      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:

      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!!!
      ShareThis
      Filed under: Web Services, Ruby on Rails, Resource Oriented, REST, Ruby | Comments (0)
      View all posts for this blog

      BTI | 44031 Pipeline Plaza Suite 325 | Ashburn, VA 20147 | 571-223-7BTI (7284) | solutions@bti360.com    

      © 2010 BTI
      Powered by SiteOrganic