Here is my Request class which holds a generics request:
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author lorddoskias
*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Request {
private List<StringStringMapEntry> parameters = new ArrayList<StringStringMapEntry>();
private List<StringStringMapEntry> headers = new ArrayList<StringStringMapEntry>();
@XmlAnyElement(lax=true)
private Object requestBody = null; // this has to reference a JAXB-enabled object
private String resourcePath;
public Request() {
}
public Object getRequestBody() {
return requestBody;
}
public void setRequestBody(Object requestBody) {
this.requestBody = requestBody;
}
Some parts are omitted for brevity but the essence is there. Then I have the following Sequence class:
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author lorddoskias
*/
@XmlRootElement
public class Sequence {
private String sequenceName;
private long numReads;
private int sequenceLength;
public Sequence() {
/* Required by JAXB */
}
public Sequence(String name, long numReads, int length) {
sequenceName = name;
this.numReads = numReads;
sequenceLength = length;
}
public long getNumReads() {
return numReads;
}
public void setNumReads(long numReads) {
this.numReads = numReads;
}
public int getSequenceLength() {
return sequenceLength;
}
public void setSequenceLength(int sequenceLength) {
this.sequenceLength = sequenceLength;
}
public String getSequenceName() {
return sequenceName;
}
public void setSequenceName(String sequenceName) {
this.sequenceName = sequenceName;
}
}
And I do something like that:
List<Sequence> seq = new ArrayList<Sequence>();
seq.add(seq1);
seq.add(seq2);
client.insertIndividualStatistics(PostRequest.assemblyIndividualStats(66, seq));
Here is the insertIndividualStatistics method:
public static Request assemblyIndividualStats(int stepId, List<Sequence> l) {
Request req = new Request();
req.setResourcePath("/stats/assembly/individual");
req.addParameter("id", String.valueOf(stepId));
req.setRequestBody(l);
return req;
}
And when I try to WebResource res = client.resource(bla).path("my-path").post(req);
I get :
Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: javax.ws.rs.WebApplicationException: javax.xml.bind.MarshalException
- with linked exception:
[javax.xml.bind.JAXBException: class uk.org.infectogenomics.model.rest.MySequenceListWrapper nor any of its super class is known to this context.]
I thought JAXB supported simple Lists if they weren't a root object? I even tried something different - wrapping the list in a simple JAXB annotated class:
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author lorddoskias
*/
@XmlRootElement
public class MySequenceListWrapper {
@XmlElement(name = "List")
private List<Sequence> list;
public MySequenceListWrapper() {/*JAXB requires it */
}
public MySequenceListWrapper(List<Sequence> list) {
this.list = list;
}
public List<Sequence> getList() {
return list;
}
}
And setting that as the requestBody but then I get a similar exception but instead of it saying that ArrayList is unrecognized it says that MySequenceListWrapper is unrecognized. I've read the article from Blaise Doughan blog and I suspect that at least in the latter case it should work since I have the appropriate XmlAnyElement annotation?