com.jayway.restassured.response
Interface ResponseBody

All Known Subinterfaces:
Response
All Known Implementing Classes:
RestAssuredResponseImpl

public interface ResponseBody


Method Summary
<T> T
as(Class<T> cls)
          Get the body and map it to a Java object.
<T> T
as(Class<T> cls, ObjectMapper mapper)
          Get the body and map it to a Java object using a specific object mapper.
 byte[] asByteArray()
          Get the body as a byte array.
 InputStream asInputStream()
          Get the body as an input stream.
 String asString()
          Get the body as a string.
 JsonPath jsonPath()
          Get a JsonPath view of the response body.
<T> T
path(String path)
          Get a value from the response body using the JsonPath or XmlPath syntax.
 String print()
          Print the response body and return it as string.
 XmlPath xmlPath()
          Get an XmlPath view of the response body.
 

Method Detail

print

String print()
Print the response body and return it as string. Mainly useful for debug purposes when writing tests.

Returns:
The body as a string.

asString

String asString()
Get the body as a string.

Returns:
The body as a string.

asByteArray

byte[] asByteArray()
Get the body as a byte array.

Returns:
The body as a array.

asInputStream

InputStream asInputStream()
Get the body as an input stream.

Returns:
The body as an input stream.

as

<T> T as(Class<T> cls)
Get the body and map it to a Java object. For JSON responses this requires that you have either
  1. Jackson, or
  2. Gson
in the classpath or for XML responses it requires JAXB to be in the classpath.
It also requires that the response content-type is either JSON or XML or that a default parser has been been set. You can also force a specific object mapper using as(Class, com.jayway.restassured.mapper.ObjectMapper).

Returns:
The object

as

<T> T as(Class<T> cls,
         ObjectMapper mapper)
Get the body and map it to a Java object using a specific object mapper. It will use the supplied mapper regardless of the response content-type.

Returns:
The object

jsonPath

JsonPath jsonPath()
Get a JsonPath view of the response body. This will let you use the JsonPath syntax to get values from the response. Example:

Assume that the GET request (to http://localhost:8080/lotto) returns JSON as:

 {
 "lotto":{
   "lottoId":5,
   "winning-numbers":[2,45,34,23,7,5,3],
   "winners":[{
     "winnerId":23,
     "numbers":[2,45,34,23,3,5]
   },{
     "winnerId":54,
     "numbers":[52,3,12,11,18,22]
   }]
  }
 }
 

You can the make the request and get the winner id's by using JsonPath:
 List winnerIds = get("/lotto").jsonPath().getList("lotto.winnders.winnerId");
 


xmlPath

XmlPath xmlPath()
Get an XmlPath view of the response body. This will let you use the XmlPath syntax to get values from the response. Example:

Imagine that a POST request to http://localhost:8080/greetXML returns:

 <greeting>
     <firstName>John</firstName>
     <lastName>Doe</lastName>
   </greeting>
 

You can the make the request and get the winner id's by using JsonPath:
 String firstName = get("/greetXML").xmlPath().getString("greeting.firstName");
 


path

<T> T path(String path)
Get a value from the response body using the JsonPath or XmlPath syntax. REST Assured will automatically determine whether to use JsonPath or XmlPath based on the content-type of the response. If no content-type is defined then REST Assured will try to look at the "default parser" if defined (RestAssured.defaultParser).

Type Parameters:
T - The return type
Parameters:
path - The json- or xml path
Returns:
The value returned by the path
See Also:
jsonPath(), xmlPath()


Copyright © 2010-2011. All Rights Reserved.