What's new in REST Assured 1.8?

REST Assured is a framework for easy testing of REST or HTTP services in Java and has recently reached version 1.8. The internals of REST Assured have undergone a major refactoring to allow JsonPath and XmlPath to be extracted to their own projects. This allows you to use these projects without depending on REST Assured itself. What does this mean and how can you benefit? Read on!

Parsing documents with Groovy GPath

Groovy has a very simple and elegant way to parse XML and JSON documents. Consider the following JSON document:


Groovy has something called a JsonSlurper which you can use to extract documents from this document:

def lotto = new JsonSlurper().parseText(lottoJson)

// Extract the lotto id
def lottoId = lotto.lottoId
// Get the list of winning numbers
def winningNumbers = lotto.'winning-numbers'
// Returns a list containing 23, 54
def winningNumbers = lotto.winners.winnerId

Now consider the following XML document:

     Pickup Truck with speed of 271kph
     Isle of Man
     Street-Legal Car at 99cm wide, 59kg
     Most Valuable Car at $15 million

For XML Groovy provides the XmlSlurper that let’s you do e.g:

def records = new XmlSlurper().parseText(recordsXml)

// Extract the name attribute of the first car
def nameOfFirstCar = records.car[0].@name
// Extract the country of the last car
def nameOfCountryForLastCar = records.car[-1].country
// Extract the years of the first two records as a list (of strings)
def years = records.car[0..1].@year

Parsing JSON documents with REST Assured JsonPath

JsonPath is essentially a wrapper around Groovys JsonSlurper that adds some additional features and makes it a little bit simpler to work with from Java. It’s a standard Maven dependency and you don’t need to configure and Groovy compilers etc, you don’t even have to know that Groovy is involved. Let’s have a look at how we can extract the very same values from the “lotto” JSON document but this time using REST Assured’s JsonPath:

JsonPath jsonPath = new JsonPath(lottoJson);

// Extract the lotto id
int lottoId = jsonPath.getInt("lotto.lottoId");
// Get the list of winning numbers,
// note that you don't need to escape 'winning-numbers'.
List winningNumbers = jsonPath.get("lotto.winning-numbers");
// Returns a list containing 23, 54
List winningNumbers = jsonPath.get("lotto.winners.winnerId");

You can see that we’ve duplicated “lotto” in all our paths. To avoid this we can set a root path:

JsonPath jsonPath = new JsonPath(lottoJson);

// Extract the lotto id
int lottoId = jsonPath.getInt("lottoId");
// Get the list of winning numbers
List winningNumbers = jsonPath.get("winning-numbers");
// Returns a list containing 23, 54
List winningNumbers = jsonPath.get("lotto.winners.winnerId");

If you’re only interested in extracting a single value you can do like this:

// "from" is statically imported from JsonPath
int lottoId = from(lottoJson).getInt("lotto.lottoId");

It’s also possible to do some more complex stuff, such as summing the total winning numbers:

int sumOfWinningNumbers = from(lottoJson).

or finding all numbers greater than 10 of the winner with id 23:

// Will return a list with 45,34 and 23
List numbers = from(lottoJson).getList(
  "lotto.winners.find {it.winnerId == 23}.numbers.findAll {it > 10}",

Object Mapping

One of the nice things with JsonPath is that it allows you to do object mapping using either Gson, Jackson or Jackson 2 and it’s also possible to roll your own. For example let’s say that we want to extract the first winner to a Java bean defined like this:

public class Winner {

    private int winnerId;
    private List numbers;

    public int getWinnerId() {
        return winnerId;

    public void setWinnerId(int winnerId) {
        this.winnerId = winnerId;

    public List getNumbers() {
        return numbers;

    public void setNumbers(List numbers) {
        this.numbers = numbers;

We can extract the first winner as a Java object:

Winner winner = from(LOTTO).getObject("lotto.winners[0]", Winner.class);


You can easily configure JsonPath to for example provider your own object mapper, configure existing object mappers, change charset etc etc. You can do this by supplying a configuration to JsonPath:

JsonPath jsonPath = new JsonPath(new File("/tmp/document.json")).
                           using(new JsonPathConfig("ISO-8859-1"));

This will instruct JsonPath to assume content type ISO-8859-1 when parsing the document at /tmp/document.json.

Summary JsonPath

Since JsonPath now can be used stand-alone without any need to depend on REST Assured itself it can be seen as an alternative to Json-Path.

Parsing XML documents with REST Assured XmlPath

Let’s revisit the XML example:

     Pickup Truck with speed of 271kph
     Isle of Man
     Street-Legal Car at 99cm wide, 59kg
     Most Valuable Car at $15 million

Using XmlPath in REST Assured we can extract some values like this:

XmlPath xmlPath = new XmlPath(recordsXml);

// Extract the name attribute of the first car
String nameOfFirstCar = xmlPath.get("car[0].@name");
// Extract the country of the last car
String nameOfCountryForLastCar = xmlPath.get("car[-1].country");
// Extract the years of the first two records as a list (of ints)
List years = xmlPath.getList("car[0..1].@year", Integer.class);

Just as with JsonPath it’s also possible to simply extract only one value:

// "from" is statically imported from XmlPath
String firstCarName = from(recordXml).get("records.car[0].@name");

You can also do some more complex stuff such as getting the year of the first record as an int:

// Year is 1931
int year = from(recordXml).

What we do here is to first converting all year attributes to a list on which we invoke the toInteger() method on all elements in the list (this is needed since the year attribute in the XML documents is represented as a String). Then we invoke the min() which will return the smallest number in the list which happens to be 1931.

Object Mapping

With XmlPath you easily map an XML document or parts of an XML document to a Java Object using JAXB (you can also roll your own). Consider the following XML:


And let’s say you have a Java bean like this:

public class Greeting {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;

And let’s say you want to map the first greeting represented in the XML document to an instance of Greeting:

Greeting greeting = from(greetingsXml).
         getObject("greetings.greeting[0]", Greeting.class);

That’s it!


You can easily configure XmlPath to for example provider your own object mapper, configure existing object mappers, change charset etc. You do this by supplying a configuration to XmlPath:

XmlPath xmlPath = new XmlPath(new File("/tmp/document.xml")).
                        using(new XmlPathConfig("ISO-8859-1"));


As you’ve hopefully seen parsing JSON and XML using JsonPath and XmlPath is very simple and powerful. Refer to the REST Assured web page for more info.

This Post Has 13 Comments

  1. Pradeep

    I am trying to use the REST Assured framework for testing our REST API. I could not find much information on publishing the test reports and integrating with CI.
    Can you please suggest.

    Thanks for the great work.
    REST Assured is amazing.

    1. Max

      Hey Pradeep,

      Not sure which CI solution you’re using.
      I’m using TestNG to organize my REST-assured cases and Maven Surefire plugin to generate reports. They work perfectly on Jenkins.

  2. Umang

    Hi Max,

    Can you please provide sample code to test rest service.


    1. Johan

      Check out the project web site, there are links to example and tutorials from the usage page.

  3. Anver

    I tried with response.getBody().jsonPath().getXXX methods. It would be good to have an additional method that will return a part of the response body as JSONDocument (String) itself based on the the path.
    The sub/new response can be used again for additional JsonPath processing. If this is already available, please share how this can be achieved.

    String str = response.getBody().jsonPath().getString(“some.path”);
    with(str).get(“more.sub.path”); // Getting Exception as Failed to parse the JSON document


    1. Johan Haleby

      Good idea. This is not available for JsonPath but it works for XmlPath (you can get a Node object in XmlPath and by using the Node object you can continue querying the document). Please add it as an improvement suggestion on the issue tracker at the project web page.

  4. june

    Is it possible to retrieve elements using Json-Path using
    wildcards ? for example, use $..book to retrieve book element no matter where it is in the document. I tried with json-path bundled with rest-assured, it did not work. If I switch to Json-Path 0.9.1 (http://code.google.com/p/json-path/), syntax like $..book works.


    1. Johan Haleby

      The JsonPath project shipped with REST Assured is NOT the same JsonPath project that’s available at http://code.google.com/p/json-path/. REST Assured’s JsonPath uses Groovy GPath expressions whereas json-path uses another syntax.

  5. SPK

    I am new to rest-assured and have the following code,

    String json = RestAssured.get(“/posts”).asString();
    JsonPath jsonPath = new JsonPath(json).using(new JsonPathConfig(“ISO-8859-1”));
    User user = JsonPath.from(json).getObject(“[0]”,User.class);

    When i run the code, i get the following error,
    java.lang.IllegalStateException: Cannot deserialize object because no JSON deserializer found in classpath. Please put either Jackson or Gson in the classpath.

    But, jackson-core is already in the classpath. Why am i getting this error

    1. Johan Haleby

      Please use the mailing list for asking questions

  6. Partha


    Is this possible to generate API Performance test reports with Rest Assured ?


    1. Johan Haleby

      REST Assured is not intended for performance testing. Have a look at JMeter, Gatling Tool or something similar.

  7. Sana Khawaja

    Also how do I use a post parameter for nested objects in jSon: example:


    “Name”: “abc”,

    “Description”: “test “,

    “Criteria”: {

    “Status”: “1,2,5,6,9,10,11”,

    “isPublic”: false



Leave a Reply