Runar Halse
Runar Halse

Reputation: 3558

Marshalling List<String> with JAX-RS

I'm used to working with jax-ws where a wsdl file is generated, and a client can then be generated based on this wsdl file and its xsd(s) using a maven plugin. Using this client is no hassle at at, and you don't have to really think about what happens in the background, like marshalling, http transfer and such.

I'm currently working on a jax-rs project using jaxb to unmarshal objects. One of the methods there returns a list of strings, but it seems that jaxb does not know how to marshal this, which is kinda surprising as it does know how to marshal a list of entities (ex, customers).

Also, I have written a client for the jax-rs service on my own, handling both http responses and unmarshalling of the payload using jaxb. Marshalling and unmarshalling with jaxb is a real hassle since it cannot automatically marshall or unmarshall list of entities that is added to its context, even less lists of strings.

I would like to know if there is some neat way to get all of this for free using restful webservices? This would have to be quite lightweight, and the clients must be easy to distribute.

Thanks! Runar

The service method that is not working using jaxrs and jaxb:

@GET
@Path("/{customerId}")
@Produces(MediaType.APPLICATION_XML)
public List<String> isCustomerLocked(@PathParam("customerId") Long customerId) {


}

Client code that attempts to marshall/unmarshall text payload. Classes added to the jaxbcontext not shown:

javax.xml.bind.Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(obj, stringwriter)

javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.unmarshal(inputstream)

Upvotes: 2

Views: 2805

Answers (1)

user647772
user647772

Reputation:

I'd use JAXB to wrap the data. For a simple List<String> this may look as overkill. But in most cases you want to un-/marshall Resource Representations, not simple objects.

Remember: REST ist not RPC!

If you really want tom un-/marshall List<String> write a JAX-RS Provider. But I'd prefer using JAXB.

S.java

@XmlRootElement
public class S {

    private String s;

    public S() {
    }

    public S(String s) {
        this.s = s;
    }

    public String getS() {
        return s;
    }

    public void setS(String s) {
        this.s = s;
    }
}

Ss.java

@XmlRootElement(name="ss-wrapper")
public class Ss {

    private List<S> ss;

    public List<S> getSs() {
        return ss;
    }

    public void setSs(List<S> ss) {
        this.ss = ss;
    }

    public Ss(List<S> ss) {
        this.ss = ss;
    }

    public Ss() {
    }
}

JAX-RS class

@Path("/strings")
@GET
@Produces(MediaType.APPLICATION_XML)
public Response getListOfStrings() {
    S s1 = new S("foo");
    S s2 = new S("bar");
    List<S> strings = new ArrayList<S>();
    strings.add(s1);
    strings.add(s2);
    Ss ss = new Ss(strings);
    return Response.ok(ss).build();
}

XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ss-wrapper>
  <ss>
    <s>foo</s>
  </ss>
  <ss>
    <s>bar</s>
  </ss>
</ss-wrapper>

Upvotes: 5

Related Questions