JAXB和List <something> </something>的又一个编组异常

时间:2012-04-26 14:58:42

标签: java xml jaxb marshalling

这是我的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;
    }

为简洁起见,省略了一些部分,但其实质是存在的。然后我有以下Sequence类:

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;
    }
}

我做了类似的事情:

List<Sequence> seq = new ArrayList<Sequence>();
        seq.add(seq1);
        seq.add(seq2);

        client.insertIndividualStatistics(PostRequest.assemblyIndividualStats(66, seq));

这是insertIndividualStatistics方法:

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;
}

当我尝试WebResource res = client.resource(bla).path("my-path").post(req);时,我得到了:

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.]

我认为JAXB支持简单列表,如果它们不是根对象?我甚至尝试了一些不同的东西 - 将列表包装在一个简单的JAXB注释类中:

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;
    }
}

然后将其设置为requestBody,但后来我得到了一个类似的异常,但不是说它无法识别ArrayList,而是说MySequenceListWrapper无法识别。我已经阅读了Blaise Doughan博客上的文章,我怀疑至少在后一种情况下它应该有效,因为我有适当的XmlAnyElement注释?

1 个答案:

答案 0 :(得分:1)

您可以使用JAX-RS ContextResolver创建一个知道JAXBContext类的MySequenceListWrapper。或者您可以使用@XmlSeeAlso注释,如下所示:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({MySequenceListWrapper.class})
public class Request {
    ...
}