Book Image

SPRING COOKBOOK

Book Image

SPRING COOKBOOK

Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Querying an existing REST service


In this recipe, from a Spring controller method, we will retrieve data from an existing REST service.

Getting ready

We will query the REST service of the previous Creating a REST service recipe.

We will convert the JSON data received from the REST service to User objects. We will use this User class:

public class User implements {    
  private String name;
  private int age;

  // ... getters and setters
}

How to do it…

Here are the steps for using a REST service:

  1. Add the Maven dependencies for Jackson in pom.xml:

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.4.2</version>
    </dependency>
    
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.4.2</version>
    </dependency>
  2. In a controller method, define the URL of the REST service to query:

    String...