Converting data between Java and XML with JAXB

In our previous example, we were processing "raw" XML received as a parameter, as well as returning "raw" XML to our client. In a real application, we would more than likely parse the XML received from the client and use it to populate a Java object. Additionally, any XML that we need to return to the client would have to be constructed from a Java object.

Converting data from Java to XML and back is such a common use case that the Java EE specification provides an API to do it. This API is the Java API for XML Binding (JAXB).

JAXB makes converting data from Java to XML transparent and trivial; all we need to do is decorate the class we wish to convert to XML with the @XmlRootElement annotation. The following code example illustrates how to do this:

package com.ensode.javaee8book.jaxrstest.entity; 
 
import java.io.Serializable; 
import javax.xml.bind.annotation.XmlRootElement; 
 
@XmlRootElement 
public class Customer implements Serializable { 
 
  private Long id; 
  private String firstName; 
  private String middleName; 
  private String lastName; 
 
  public Customer() { 
  } 
 
  public Customer(Long id, String firstName, 
      String middleInitial, String lastName) { 
    this.id = id; 
    this.firstName = firstName; 
    this.middleName = middleInitial; 
    this.lastName = lastName; 
  } 
 
  //getters and setters omitted for brevity 

  @Override 
  public String toString() { 
    return "id = " + getId() + "
firstName = " + getFirstName() 
        + "
middleName = " + getMiddleName() + "
lastName = " 
        + getLastName(); 
  } 
} 

As we can see, other than the @XmlRootElement annotation at the class level, there is nothing unusual about the above Java class.

Once we have a class that we have decorated with the @XmlRootElement annotation, we need to change the parameter type of our web service from String to our custom class:

package com.ensode.javaee8book.jaxbxmlconversion.service; 
 
import com.ensode.jaxbxmlconversion.entity.Customer; 
import javax.ws.rs.Consumes; 
import javax.ws.rs.DELETE; 
import javax.ws.rs.GET; 
import javax.ws.rs.POST; 
import javax.ws.rs.PUT; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
 
@Path("customer") 
public class CustomerResource { 
 
  private Customer customer; 
 
  public CustomerResource() { 
    //"fake" the data, in a real application the data 
    //would come from a database. 
    customer = new Customer(1L, "David", 
        "Raymond", "Heffelfinger"); 
  } 
 
  @GET 
  @Produces("text/xml") 
  public Customer getCustomer() { 
    //in a "real" RESTful service, we would retrieve data from a   
database //then return an XML representation of the data. System.out.println("--- " + this.getClass().getCanonicalName() + ".getCustomer() invoked"); return customer; } @POST @Consumes("text/xml") public void updateCustomer(Customer customer) { //in a "real" RESTful service, JAXB would parse the XML //received in the customer XML parameter, then update //a row in the database. System.out.println("--- " + this.getClass().getCanonicalName() + ".updateCustomer() invoked"); System.out.println("---- got the following customer: " + customer); } @PUT @Consumes("text/xml") public void createCustomer(Customer customer) { //in a "real" RESTful service, we would insert //a new row into the database with the data in the //customer parameter System.out.println("--- " + this.getClass().getCanonicalName() + ".createCustomer() invoked"); System.out.println("customer = " + customer); } @DELETE @Consumes("text/xml") public void deleteCustomer(Customer customer) { //in a "real" RESTful service, we would delete a row //from the database corresponding to the customer parameter System.out.println("--- " + this.getClass().getCanonicalName() + ".deleteCustomer() invoked"); System.out.println("customer = " + customer); } }

As we can see, the difference between this version of our RESTful web service and the previous one is that all parameter types and return values have been changed from String to Customer. JAXB takes care of converting our parameters and return types to and from XML as appropriate. When using JAXB, an object of our custom class is automatically populated with data from the XML data sent from the client, similarly, return values are transparently converted to XML.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.70.101