Chapter 21. Examples for Chapter 6
In Chapter 3, you saw a quick overview on how to write a simple JAX-RS service. You might have noticed that we needed a lot of code to process incoming and outgoing XML data. In Chapter 6, you learned that all this handcoded marshalling code is unnecessary. JAX-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
class
Customer
{
private
int
id
;
private
String
firstName
;
private
String
lastName
;
private
String
street
;
private
String
city
;
private
String
state
;
private
String
zip
;
private
String
country
;
@XmlAttribute
public
int
getId
()
{
return
id
;
}
public
void
setId
(
int
id
)
{
this
.
id
=
id
;
}
@XmlElement
(
name
=
"first-name"
)
public
String
getFirstName
()
{
return
firstName
;
}
public
void
setFirstName
(
String
firstName
)
{
this
.
firstName
=
firstName
;
}
@XmlElement
(
name
=
"last-name" ...
Get RESTful Java with JAX-RS 2.0, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.