Java Reference
In-Depth Information
customer . getId ())). build ();
}
@GET
@Path ( "{id}" )
@Produces ( "application/xml" )
public
public Customer getCustomer ( @PathParam ( "id" ) int
int id ) {
Customer customer = customerDB . get ( id );
iif ( customer == null
null ) {
throw
throw new
new WebApplicationException ( Response . Status . NOT_FOUND );
}
return
return customer ;
}
@PUT
@Path ( "{id}" )
@Consumes ( "application/xml" )
public
public void
void updateCustomer ( @PathParam ( "id" ) int
int id ,
Customer update ) {
Customer current = customerDB . get ( id );
iif ( current == null
null )
throw
throw new
new WebApplicationException ( Response . Status . NOT_FOUND );
current . setFirstName ( update . getFirstName ());
current . setLastName ( update . getLastName ());
current . setStreet ( update . getStreet ());
current . setState ( update . getState ());
current . setZip ( update . getZip ());
current . setCountry ( update . getCountry ());
}
}
If you compare this with the CustomerResource class in ex03_1 , you'll see that the code in
this example is much more compact. There is no handcoded marshalling code, and our meth-
ods are dealing with Customer objects directly instead of raw strings.
The Client Code
The client code can also now take advantage of automatic JAXB marshalling. All JAX-RS
2.0 client implementations must support JAXB as a mechanism to transmit XML on the cli-
ent side. Let's take a look at how the client code has changed from ex03_1 :
@Test
public
public void
void testCustomerResource () throws
throws Exception
Search WWH ::




Custom Search