DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. REST Spring 3.0 - Simple Introduction

REST Spring 3.0 - Simple Introduction

Alex Staveley user avatar by
Alex Staveley
·
Dec. 12, 11 · Interview
Like (0)
Save
Tweet
Share
15.12K Views

Join the DZone community and get the full member experience.

Join For Free
This post is going to be a very simple introduction to the RestTemplate API introduced in Spring 3.0.
Dogs know how to REST!
The RestTemplate is similar to other Spring templates such as JmsTemplate and JdbcTemplate in that Spring eliminates a lot of boot strap code and thus makes your code much cleaner.  When applications use the RestTemplate they do not need to worry about HTTP connections, that is all encapsulated by the template. They also get a range of APIs from the RestTemplate which correspond to the well know HTTP methods (GET, PUT, POST, DELETE, HEAD, OPTIONS).  These APIs are overloaded to cater for things  like different ways of passing parameters to the actual REST API.  Ok, so let's look a very simple example.  Suppose we want to invoke a well know Twitter REST API to check the Twitter timeline for the user "dublintech".  This API would takes form: http://twitter.com/statuses/user_timeline.xml?id=dublintech

Fire it into your web browser and you'll see you get back the timeline for dublintech in XML.  But you don't want to fire it into your browser, you want to invoke the API in a Java program!  Let's do that.

import org.springframework.web.client.RestTemplate;
 
public class Spring3RestTest {
 
    public static void main(String args[]) {
        Spring3RestTest test = new Spring3RestTest();
        test.getTimeline();
    }
     
    private void getTimeline() {
        RestTemplate restTemplate = new RestTemplate();
        String response =
                restTemplate.getForObject("http://twitter.com/statuses/user_timeline.xml?id=dublintech", String.class, new Object[]{});
        System.out.println("Response is=" + response);
    }
}

That's it. Isn't that incredibly simple. No HTTP Connection handling to worry about. Import one Spring 3.0 class, use it and that's it!  Yes, this post was a very simple introduction. In the real world, if you were using this API you'd obviously be doing something a little more sophisticated for example using XPath to get specific information out of the XML returned. But we don't need to cover that.  The point of this post was really to point how easy it is to use some of the REST 3.0 Spring features. In future posts we'll be looking at more advanced features.

 For now - keep it simple!
References:
1. http://static.springsource.org/spring/docs/3.0.x/javadoc-api/
2. http://blog.springsource.org/2009/03/27/rest-in-spring-3-resttemplate/
3. https://dev.twitter.com/docs/api Twitter REST API
4. Roy Fielding's original REST Paper: http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

 

From http://dublintech.blogspot.com/2011/11/rest-spring-30-simple-introduction.html

REST Web Protocols Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Spring Boot vs Eclipse MicroProfile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative
  • A Beginner’s Guide To Styling CSS Forms
  • Front-End Troubleshooting Using OpenTelemetry
  • What Is API-First?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: