Jersey uses an XML message

I want to send a message to Jersey Support. What is the standard way to do this?

@Post
@Consumes(MediaType.Application_xml)
public Response method(??){}
+5
source share
2 answers

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. , .

+6

:

@POST
@Consumes({"application/xml", "application/json"})
public Response create(@Context UriInfo uriInfo, Customer entity) {
    entityManager.persist(entity);
    entityManager.flush();

    UriBuilder uriBuilder = uriBuiler.path(String.valueOf(entity.getId()));
    return Response.created(uriBuilder.build()).build();
}
+4

All Articles