How can I output an XML file to a Java REST web service so that another application can consume that XML?

I need to output an XML file from one application to another, but I will not need to write this XML anywhere and then read that file in another application.

Both are Java applications and (so far!) I am using XStream.

How can i do this?

+3


source to share


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


Another approach for high load applications is to use google ProtoBuf instead of XML format - this allows you to minimize traffic between applications and increase performance. From my point of view, XML for data transfer is not a good idea.



+2


source







All Articles