Rest–Assuredis a Java-based library that is used to test RESTful Web Services. REST-assured was designed to simplify the testing and validation of REST APIs. It takes influence from testing techniques used in dynamic languages such as Ruby and Groovy.
As of Java 11, JAXB is not part of the JRE anymore, and you need to configure the relevant libraries via your dependency management system, for example, either Maven or Gradle.
Configure the Java compiler level to be at least 11 and add the JAXB Version 3 dependencies to your pom file.
Now, let us create the Java Objects (POJO) of above XML.
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "EmployeeDetail")
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
private String firstName;
private String lastName;
private int age;
private double salary;
private String designation;
private String contactNumber;
private String emailId;
private String gender;
private String maritalStatus;
public Employee() {
super();
}
// Getter and setter methods
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getMaritalStatus() {
return maritalStatus;
}
public void setMaritalStatus(String maritalStatus) {
this.maritalStatus = maritalStatus;
}
@Override
public String toString() {
return "Employee [FirstName=" + firstName + ", LastName=" + lastName + ", Age=" + age + ", Salary=" + salary
+ ", Designation=" + designation + ", ContactNumber=" + contactNumber + ", EmailId=" + emailId
+ ", Gender=" + gender + ", MaritalStatus=" + maritalStatus + "]";
}
}
Let’s create a simple program using the JAXBContextwhich provides an abstraction for managing the XML/Java binding information necessary to implement the JAXB binding framework operations and unmarshal.
When we run the code above, we may check the console output to verify that we have successfully converted XML data into a Java object:
The Unmarshaller class governs the process of deserializing XML data into newly created Java content trees, optionally validating the XML data as it is unmarshalled. It provides overloading of unmarshal methods for many input kinds.
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
As of Java 11, JAXB is not part of the JRE anymore, and you need to configure the relevant libraries via your dependency management system, for example, either Maven or Gradle.
Configure the Java compiler level to be at least 11 and add the JAXB Version 3 dependencies to your pom file.
Now, let us create the Java Objects (POJO) of the above XML.
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "EmployeeDetail")
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
private String firstName;
private String lastName;
private int age;
private double salary;
private String designation;
private String contactNumber;
private String emailId;
private String gender;
private String maritalStatus;
public Employee() {
super();
}
// Getter and setter methods
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getMaritalStatus() {
return maritalStatus;
}
public void setMaritalStatus(String maritalStatus) {
this.maritalStatus = maritalStatus;
}
@Override
public String toString() {
return "Employee [FirstName=" + firstName + ", LastName=" + lastName + ", Age=" + age + ", Salary=" + salary
+ ", Designation=" + designation + ", ContactNumber=" + contactNumber + ", EmailId=" + emailId
+ ", Gender=" + gender + ", MaritalStatus=" + maritalStatus + "]";
}
}
Let’s create a simple program using the JAXBContextwhich provides an abstraction for managing the XML/Java binding information necessary to implement the JAXB binding framework operations.
import java.io.StringWriter;
import org.junit.Test;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.PropertyException;
public class JAXBSerialization {
@Test
public void serializationTest1() {
try {
Employee employee = new Employee();
employee.setFirstName("Terry");
employee.setLastName("Mathew");
employee.setAge(30);
employee.setSalary(75000);
employee.setDesignation("Manager");
employee.setContactNumber("+919999988822");
employee.setEmailId("abc@test.com");
employee.setMaritalStatus("married");
employee.setGender("female");
// Create JAXB Context
JAXBContext context = JAXBContext.newInstance(Employee.class);
// Create Marshaller
Marshaller jaxbMarshaller = context.createMarshaller();
// Required formatting
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// Print XML String to Console
StringWriter sw = new StringWriter();
// Write XML to StringWriter
jaxbMarshaller.marshal(employee, sw);
// Verify XML Content
String xmlContent = sw.toString();
System.out.println(xmlContent);
} catch (PropertyException e) {
e.printStackTrace();
} catch (JAXBException e) {
}
}
}
When we run the code above, we may check the console output to verify that we have successfully converted the Java object into XML:
The Marshaller class is responsible for governing the process of serializing Java content trees back into XML data.
I hope this has helped you to understand the use of JAXB Version 3.
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
The previous tutorials explain how to use JAXB(Java Architecture for XML Binding) to parse XML documents to Java objects and vice versa. This is also calledMarshalling and Unmarshalling.
This tutorial explains @XmlElementWrapperAnnotation.
Configure the Java compiler level to be at least 11 and add the JAXB dependencies to the pom file.
@XmlElementWrapper generates a wrapper element around XML representation. This is primarily intended to be used to produce a wrapper XML element around collections.
This annotation can be used with the following annotations – XmlElement, XmlElements, XmlElementRef, XmlElementRefs, XmlJavaTypeAdapter.
@XmlElementWrapper and @XmlElement (Wrapped collection)
Let us understand this with the help of an example shown below.
@XmlRootElement(name = "CustomerDetails")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
private int id;
private String name;
private int yearOfBirth;
private String emailId;
private String streetAddress;
private String postcode;
@XmlElementWrapper(name = "emergencyContacts")
@XmlElement(name = "Contact")
private List<String> emergencyContacts;
public Customer() {
super();
}
public Customer(int id, String name, int yearOfBirth, String emailId, String streetAddress, String postcode,
List<String> emergencyContacts) {
super();
this.id = id;
this.name = name;
this.yearOfBirth = yearOfBirth;
this.emailId = emailId;
this.streetAddress = streetAddress;
this.postcode = postcode;
this.emergencyContacts = emergencyContacts;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYearOfBirth() {
return yearOfBirth;
}
public void setYearOfBirth(int yearOfBirth) {
this.yearOfBirth = yearOfBirth;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getStreetAddress() {
return streetAddress;
}
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
public List<String> getEmergencyContacts() {
return emergencyContacts;
}
public void setEmergencyContacts(List<String> emergencyContacts) {
this.emergencyContacts = emergencyContacts;
}
}
Now, let us create a Test to convert these Java Objects to XML.