Java Reference
In-Depth Information
.
accept
(
"application/json"
)
.
get
();
try
try
{
iif
(
response
.
getStatus
() ==
200
) {
Customer customer
=
response
.
readEntity
(
Customer
.
class
);
}
}
finally
finally
{
response
.
close
();
}
In this example, we invoke an HTTP GET to obtain a
Response
object. We check that the
status is
OK
and if so, extract a
Customer
object from the returned JSON document by invok-
ing
Response.readEntity()
. The
readEntity()
method matches up the requested Java
type and the response content with an appropriate
MessageBodyReader
. This method can be
invoked only once unless you buffer the response with the
bufferEntity()
method. For ex-
ample:
Response response
=
client
.
target
(
"http://commerce.com/customers/123"
)
.
accept
(
"application/json"
)
.
get
();
try
try
{
iif
(
response
.
getStatus
() ==
200
) {
response
.
bufferEntity
();
Customer customer
=
response
.
readEntity
(
Customer
.
class
);
Map rawJson
=
response
.
readEntity
(
Map
.
class
);
}
}
finally
finally
{
response
.
close
();
}
In this example, the call to
bufferEntity()
allows us to extract the HTTP response content
into different Java types, the first type being a
Customer
and the second a
java.util.Map
that represents raw JSON data. If we didn't buffer the entity, the second
readEntity()
call
would result in an
IllegalStateException
.