Java Reference
In-Depth Information
import
import
javax.ws.rs.client.ClientBuilder
javax.ws.rs.client.ClientBuilder
;
import
import
javax.ws.rs.client.Client
javax.ws.rs.client.Client
;
import
import
javax.ws.rs.client.Entity
javax.ws.rs.client.Entity
;
import
import
javax.ws.rs.core.Response
javax.ws.rs.core.Response
;
public
public class
class
MyClient
MyClient
{
public
public static
throws
Exception
{
Client client
=
ClientBuilder
.
newClient
();
try
static
void
void
main
(
String
[]
args
)
throws
try
{
System
.
out
.
println
(
"*** Create a new Customer ***"
);
String xml
=
"<customer>"
+
"<first-name>Bill</first-name>"
+
"<last-name>Burke</last-name>"
+
"<street>256 Clarendon Street</street>"
+
"<city>Boston</city>"
+
"<state>MA</state>"
+
"<zip>02115</zip>"
+
"<country>USA</country>"
+
"</customer>"
;
Response response
=
client
.
target
(
"http://localhost:8080/services/customers"
)
.
request
().
post
(
Entity
.
xml
(
xml
));
iif
(
response
.
getStatus
() !=
201
)
throw
throw new
new
RuntimeException
(
"Failed to create"
);
String location
=
response
.
getLocation
().
toString
();
System
.
out
.
println
(
"Location: "
+
location
);
response
.
close
();
System
.
out
.
println
(
"*** GET Created Customer **"
);
String customer
=
client
.
target
(
location
).
request
().
get
(
String
.
class
);
System
.
out
.
println
(
customer
);
String updateCustomer
=
"<customer>"
+
"<first-name>William</first-name>"
+
"<last-name>Burke</last-name>"
+
"<street>256 Clarendon Street</street>"
+
"<city>Boston</city>"
+
"<state>MA</state>"
+
"<zip>02115</zip>"
+
"<country>USA</country>"
+
"</customer>"
;
response
=
client
.
target
(
location
)
.
request
()
.
put
(
Entity
.
xml
(
updateCustomer
));