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")publicclassCustomer{privateintid;privateStringfirstName;privateStringlastName;privateStringstreet;privateStringcity;privateStringstate;privateStringzip;privateStringcountry;@XmlAttributepublicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}@XmlElement(name="first-name")publicStringgetFirstName(){returnfirstName;}publicvoidsetFirstName(StringfirstName){this.firstName=firstName;}@XmlElement(name="last-name" ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access