Java Reference
In-Depth Information
Invocation
The previous examples are how you're going to typically interact with the Client API. JAX-
RS has an additional invocation model that is slightly different. You can create full
Invoca-
tion
objects that represent the entire HTTP request without invoking it. There's a few addi-
tional methods on
Invocation.Builder
that help you do this:
public
public interface
interface
Invocation
Invocation
{
...
public
public interface
extends
SyncInvoker
,
Configurable
<
Builder
> {
Invocation
build
(
String method
);
Invocation
build
(
String method
,
Entity
<?>
entity
);
Invocation
buildGet
();
Invocation
buildDelete
();
Invocation
buildPost
(
Entity
<?>
entity
);
Invocation
buildPut
(
Entity
<?>
entity
);
...
interface
Builder
Builder
extends
}
}
The
buildXXX()
methods fill in the HTTP method you want to use and finish up building
the request by returning an
Invocation
instance. You can then execute the HTTP request by
calling one of the
invoke()
methods:
package
package
javax
.
ws
.
rs
.
client
;
public
public interface
interface
Invocation
Invocation
{
public
public
Response
invoke
();
public
public
<
T
>
T invoke
(
Class
<
T
>
responseType
);
public
public
<
T
>
T invoke
(
GenericType
<
T
>
responseType
);
...
}
So what is the use of this invocation style? For one, the same
Invocation
object can be used
for multiple requests. Just prebuild your
Invocation
instances and reuse them as needed.
Also, since
invoke()
is a generic method, you could queue up
Invocation
instances or use
them with the execute pattern. Let's see an example:
Invocation generateReport
=
client
.
target
(
"http://commerce.com/orders/report"
)
.
queryParam
(
"start"
,
"now - 5 minutes"
)
.
queryParam
(
"end"
,
"now"
)
.
request
()
.
accept
(
"application/json"
)
.
buildGet
();