Suppose you have a java bean say employee bean, for example. Add tags to report
@XmlRootElement (name = "Employee")
public class Employee {
String employeeName;
@XmlElement
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
}
@XmlRootElement reports that this will be the main tag in xml. In this case, you can also specify a name for the main tag.
@XmlElement reports that it will be a sub tag inside the root tag
Say the xml sample that appears as part of the body in the post request will look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<Employee>
<employeeName>Jack</employeeName>
</Employee>
When writing a web service to access this kind of xml, we can write the following method.
@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public Response getEmployee(Employee employee) {
employee.setEmployeeName(employee.getEmployeeName() + " Welcome");
return Response.status(Status.OK).entity(employee).build();
}
When you call this service, you will get the following xml as part of the response.
<Employee>
<employeeName> Jack Welcome </employeeName>
</Employee>
@Xml... , .
JSON, JSON, MediaType.APPLICATION_JSON APPLICATION_XML
, xml xml HTTP.
, .