Java Reference
In-Depth Information
ic methods annotated with
@AfterClass
are executed once after all
@Test
methods have run.
The
closeClient()
method cleans up our
Client
object by invoking
close()
after all tests
have run. This is a nice way of putting repetitive initialization and cleanup code that is
needed for each test in one place.
The rest of the class is pretty straightforward and similar to
ex03_1
. I'll highlight only the in-
teresting parts:
String patchCustomer
=
"<customer>"
+
"<first-name>William</first-name>"
+
"</customer>"
;
response
=
client
.
target
(
location
)
.
request
().
method
(
"PATCH"
,
Entity
.
xml
(
patchCustomer
));
iif
(
response
.
getStatus
() !=
204
)
throw
new
RuntimeException
(
"Failed to update"
);
response
.
close
();
throw new
To make a
PATCH
HTTP invocation, we use the
javax.ws.rs.client.SyncInvoker.method()
method. The parameters to this method are
a string denoting the HTTP method you want to invoke and the entity you want to pass as the
message body. Simple as that.
Example ex04_2: @Path with Expressions
For this section, I'll illustrate the use of an
@Path
annotation with regular expressions. The
example is a direct copy of the code in
ex03_1
with a few minor modifications.
Build and Run the Example Program
Perform the following steps:
1. Open a command prompt or shell terminal and change to the
ex04_2
directory of the
workbook example code.
2. Make sure your PATH is set up to include both the JDK and Maven, as described in
3. Perform the build and run the example by typing
maven install
.
The Server Code
The
CustomerResource
class copied from the
ex03_1
example is pretty much the same in
ex04_2
, except that a few of the
@Path
expressions have been modified. I also added an extra