Java Reference
In-Depth Information
Chapter 21. Examples for
Chapter 6
might have noticed that we needed a lot of code to process incoming and outgoing XML
RS has a number of built-in content handlers that can do the processing for you. You also
learned that if these prepackaged providers do not meet your requirements, you can write
your own content handler.
There are two examples in this chapter. The first rewrites the
ex03_1
example to use JAXB
instead of handcoded XML marshalling. The second example implements a custom content
handler that can send serialized Java objects over HTTP.
Example ex06_1: Using JAXB
This example shows how easy it is to use JAXB and JAX-RS to exchange XML documents
over HTTP. The
com.restfully.shop.domain.Customer
class is the first interesting piece
of the example.
src/main/java/com/restfully/shop/domain/Customer.java
@XmlRootElement
(
name
=
"customer"
)
public
public class
class
Customer
Customer
{
private
private
int
int
id
;
private
private
String firstName
;
private
private
String lastName
;
private
private
String street
;
private
private
String city
;
private
private
String state
;
private
private
String zip
;
private
private
String country
;
@XmlAttribute
public
public
int
int
getId
() {
return
return
id
;
}
public
public
void
void
setId
(
int
int
id
) {
this
this
.
id
=
id
;
}
@XmlElement
(
name
=
"first-name"
)