Giter Site home page Giter Site logo

mnenad / gs-consuming-rest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spring-guides/gs-consuming-rest

0.0 2.0 0.0 334 KB

Consuming a RESTful Web Service :: Learn how to retrieve web page data with Spring's RestTemplate.

Home Page: https://spring.io/guides/gs/consuming-rest/

Java 85.34% Shell 14.66%

gs-consuming-rest's Introduction

tags projects
rest

This guide walks you through the process of creating an application that consumes a RESTful web service.

What you’ll build

You’ll build an application that uses Spring’s RestTemplate to retrieve a random Spring Boot quotation at http://gturnquist-quoters.cfapps.io/api/random.

Fetch a REST resource

With project setup complete, you can create a simple application that consumes a RESTful service.

A RESTful service has been stood up at http://gturnquist-quoters.cfapps.io/api/random. It randomly fetches quotes about Spring Boot and returns them as a JSON document.

If you request that URL through your web browser or curl, you’ll receive a JSON document that looks something like this:

{
   type: "success",
   value: {
      id: 10,
      quote: "Really loving Spring Boot, makes stand alone Spring apps easy."
   }
}

Easy enough, but not terribly useful when fetched through a browser or through curl.

A more useful way to consume a REST web service is programmatically. To help you with that task, Spring provides a convenient template class called RestTemplate. RestTemplate makes interacting with most RESTful services a one-line incantation. And it can even bind that data to custom domain types.

First, create a domain class to contain the data that you need. If all you need to know are Pivotal’s name, phone number, website URL, and what the pivotalsoftware page is about, then the following domain class should do fine:

src/main/java/hello/Quote.java

link:complete/src/main/java/hello/Quote.java[role=include]

As you can see, this is a simple Java class with a handful of properties and matching getter methods. It’s annotated with @JsonIgnoreProperties from the Jackson JSON processing library to indicate that any properties not bound in this type should be ignored.

It also uses Project Lombok’s @Data annotation, which provides a getter, a setter, a toString, and other supporting methods.

An additional class is needed to embed the inner quotation itself.

src/main/java/hello/Value.java

link:complete/src/main/java/hello/Value.java[role=include]

This uses the same annotations but simply maps onto other data fields.

Make the application executable

Although it is possible to package this service as a traditional WAR file for deployment to an external application server, the simpler approach demonstrated below creates a standalone application. You package everything in a single, executable JAR file, driven by a good old Java main() method. Along the way, you use Spring’s support for embedding the Tomcat servlet container as the HTTP runtime, instead of deploying to an external instance.

Now you can write the Application class that uses RestTemplate to fetch the data from our Spring Boot quotation service.

src/main/java/hello/Application.java

link:complete/src/main/java/hello/Application.java[role=include]

Because the Jackson JSON processing library is in the classpath, RestTemplate will use it (via a message converter) to convert the incoming JSON data into a Quote object. From there, the contents of the Quote object will be logged to the console.

Here you’ve only used RestTemplate to make an HTTP GET request. But RestTemplate also supports other HTTP verbs such as POST, PUT, and DELETE.

You should see the following output:

2015-09-23 14:22:26.415  INFO 23613 --- [main] hello.Application  : Quote{type='success', value=Value{id=12, quote='@springboot with @springframework is pure productivity! Who said in #java one has to write double the code than in other langs? #newFavLib'}}

Summary

Congratulations! You have just developed a simple REST client using Spring.

gs-consuming-rest's People

Contributors

btalbott avatar cbeams avatar gregturn avatar habuma avatar joshlong avatar p-g714 avatar royclarkson avatar tsuyo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.