Package com.jcabi.http.response
Class JsonResponse
- java.lang.Object
-
- com.jcabi.http.response.JsonResponse
-
- All Implemented Interfaces:
Response
@Immutable public final class JsonResponse extends Object
JSON response.This response decorator is able to parse HTTP response body as a JSON document and manipulate with it afterwords, for example:
String name = new JdkRequest("http://my.example.com") .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON) .fetch() .as(JsonResponse.class) .json() .readObject() .getString("name");The class is immutable and thread-safe.
- Since:
- 0.8
-
-
Constructor Summary
Constructors Constructor Description JsonResponse(Response resp)Public ctor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T extends Response>
Tas(Class<T> type)Convert it to another type, by encapsulation.JsonResponseassertJson(String element)Verifies the JSON data against the element identifier argument, and throwsAssertionErrorin case of mismatch.Requestback()Get back to the request it's related to.byte[]binary()Raw body as a an array of bytes.Stringbody()Get body as a string, assuming it'sUTF-8(if there is something else that can't be translated into a UTF-8 string a runtime exception will be thrown).Map<String,List<String>>headers()Get a collection of all headers.jakarta.json.JsonReaderjson()Read body as JSON.Stringreason()Get status line reason phrase.intstatus()Get status of the response as a positive integer number.StringtoString()
-
-
-
Constructor Detail
-
JsonResponse
public JsonResponse(Response resp)
Public ctor.- Parameters:
resp- Response
-
-
Method Detail
-
assertJson
public JsonResponse assertJson(String element)
Verifies the JSON data against the element identifier argument, and throwsAssertionErrorin case of mismatch.- Parameters:
element- Element in the JSON data of this object- Returns:
- This object
-
json
public jakarta.json.JsonReader json()
Read body as JSON.- Returns:
- Json reader
-
back
public final Request back()
Description copied from interface:ResponseGet back to the request it's related to.
-
status
public final int status()
Description copied from interface:ResponseGet status of the response as a positive integer number.
-
reason
public final String reason()
Description copied from interface:ResponseGet status line reason phrase.
-
headers
public final Map<String,List<String>> headers()
Description copied from interface:ResponseGet a collection of all headers.
-
body
public String body()
Description copied from interface:ResponseGet body as a string, assuming it'sUTF-8(if there is something else that can't be translated into a UTF-8 string a runtime exception will be thrown).DISCLAIMER: The only encoding supported here is UTF-8. If the body of response contains any chars that can't be used and should be replaced with a "replacement character", a
RuntimeExceptionwill be thrown. If you need to use some other encodings, useResponse.binary()instead.
-
binary
public final byte[] binary()
Description copied from interface:ResponseRaw body as a an array of bytes.
-
-