2

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

为简洁起见省略了某些部分,但精髓就在那里。然后我有以下序列类:

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 注释?

4

1 回答 1

1

您可以使用 JAX-RSContextResolver来制作JAXBContext知道MySequenceListWrapper该类的内容。或者您可以@XmlSeeAlso按如下方式利用注释:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({MySequenceListWrapper.class})
public class Request {
    ...
}
于 2012-04-26T15:57:08.310 回答