Java Reference
In-Depth Information
@GET
@Path
(
"{id}"
)
@Produces
(
"text/plain"
)
public
public
Customer
getCustomerString
(
@PathParam
(
"id"
)
int
int
id
)
{
return
return
getCustomer
(
id
).
toString
();
}
The Client Code
The client code for this example executes various HTTP GET requests to retrieve different
representations of a
Customer
. Each request sets the
Accept
header a little differently so that
it can obtain a different representation. For example:
src/test/java/com/restfully/shop/test/CustomerResourceTest.java
public
public class
class
CustomerResourceTest
CustomerResourceTest
{
@Test
public
public
void
void
testCustomerResource
()
throws
throws
Exception
{
...
initialization code
...
System
.
out
.
println
(
"*** GET XML Created Customer **"
);
String xml
=
client
.
target
(
location
).
request
()
.
accept
(
MediaType
.
APPLICATION_XML_TYPE
)
.
get
(
String
.
class
);
System
.
out
.
println
(
xml
);
System
.
out
.
println
(
"*** GET JSON Created Customer **"
);
String json
=
client
.
target
(
location
).
request
()
.
accept
(
MediaType
.
APPLICATION_JSON_TYPE
)
.
get
(
String
.
class
);
System
.
out
.
println
(
json
);
}
}
The
SyncInvoker.accept()
method is used to initialize the
Accept
header. The client ex-
tracts a
String
from the HTTP response so it can show you the request XML or JSON.
Build and Run the Example Program
Perform the following steps:
1. Open a command prompt or shell terminal and change to the
ex09_1
directory of the
workbook example code.