How can I output an XML file to a Java REST web service so that another application can consume that XML?
2 answers
Note. I am EclipseLink JAXB (MOXy) and a member of the JAXB team (JSR-222) .
JAXB (JSR-222) is the default binding layer for Java API for RESTful Web Services (JAX-RS) . This means that you can simply create a service that returns a POJO and all conversions to / from XML will be handled for you.
Below is an example of a JAX-RS service that scans an instance Customer
using JPA and returns it in XML. The JAX-RS implementation will allow JAXB to do the actual transformation automatically.
package org.example;
import java.util.List;
import javax.ejb.*;
import javax.persistence.*;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Stateless
@LocalBean
@Path("/customers")
public class CustomerService {
@PersistenceContext(unitName="CustomerService",
type=PersistenceContextType.TRANSACTION)
EntityManager entityManager;
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("{id}")
public Customer read(@PathParam("id") long id) {
return entityManager.find(Customer.class, id);
}
}
Complete example
+3
source to share