Managing JSON Responses
When your HTTP Response
contains a JSON document, it is convenient to use JsonResponse
decorator to manipulate it, for example:
String name = new JdkRequest("http://my.example.com") .fetch() .as(JsonResponse.class) .json() // convert HTTP body to javax.json.JsonReader .readJsonObject().getString("name");
Method JsonResponse#json()
converts HTTP body to an instance of class JsonReader
.
Don't forget to add these two dependencies to your classpath:
<dependency> <groupId>javax.json</groupId> <artifactId>javax.json-api</artifactId> <version>1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.json</artifactId> <version>1.0.3</version> <scope>runtime</scope> </dependency>
Read also about RestResponse
, which helps you to manage default HTTP response concepts.