Java Reference
In-Depth Information
{
System . out . println ( "*** Create a new Customer ***" );
Customer newCustomer = new
new Customer ();
newCustomer . setFirstName ( "Bill" );
newCustomer . setLastName ( "Burke" );
newCustomer . setStreet ( "256 Clarendon Street" );
newCustomer . setCity ( "Boston" );
newCustomer . setState ( "MA" );
newCustomer . setZip ( "02115" );
newCustomer . setCountry ( "USA" );
We start off by allocating and initializing a Customer object with the values of the new cus-
tomer we want to create.
Response response =
client . target ( "http://localhost:8080/services/customers" )
. request (). post ( Entity . xml ( newCustomer ));
iif ( response . getStatus () != 201 )
throw
throw new
new RuntimeException ( "Failed to create" );
The code for posting the new customer looks exactly the same as ex03_1 except that instead
of initializing a javax.ws.rs.client.Entity with an XML string, we are using the Cus-
tomer object. The JAX-RS client will automatically marshal this object into an XML string
and send it across the wire.
Changes to pom.xml
JBoss RESTEasy is broken up into a bunch of smaller JARs so that you can pick and choose
which features of RESTEasy to use. Because of this, the core RESTEasy JAR file does not
have the JAXB content handlers. Therefore, we need to add a new dependency to our
pom.xml file:
<dependency>
<dependency>
<groupId>
<groupId> org.jboss.resteasy </groupId>
</groupId>
<artifactId>
<artifactId> resteasy-jaxb-provider </artifactId>
</artifactId>
<version>
<version> 1.2 </version>
</version>
</dependency>
Adding this dependency will add the JAXB provider to your project. It will also pull in any
third-party dependency RESTEasy needs to process XML. If you are deploying on a Java EE
application server like Wildfly or JBoss, you will not need this dependency; JAXB support is
preinstalled.
Search WWH ::




Custom Search