3

I need to output an XML file from one application to another, but I'd like not to have to write this XML somewhere and then reading this file on the other application.

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

How can I do it?

2 Answers 2

3

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

JAXB (JSR-222) is the default binding layer for The Java API for RESTful Web Services (JAX-RS). This means you can just create a service that returns POJOs and all the to/from XML conversion will be handled for you.

Below is an example JAX-RS service that looks up an instance of Customer using JPA and returns it to XML. The JAX-RS implementation will leverage JAXB to do the actual conversion 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);
    }

}

Full Example

Sign up to request clarification or add additional context in comments.

Comments

2

Another approach, for heavy loaded applications, is to using google ProtoBuf instead of the XML format - this allow You to minimize traffic between applications and increase perfomance.From my perspective, XML for data transfer is not a good idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.