我已经通过注释现有的Java域模型类创建的XML架构,现在当我尝试使用JAXB来解读我的Restlet web服务遇到错误的主机无论内收到代表什么,我似乎尝试。我是新来的都restlets和JAXB这样指着我同时使用将是有益的体面的例子的方向只有一个,我设法找到至今在这里:的实施例

我的错误是:

如果我尝试使用restlet.ext.jaxb JaxbRepresentation:

@Override 
public void acceptRepresentation(Representation representation)
    throws ResourceException {
JaxbRepresentation<Order> jaxbRep = new JaxbRepresentation<Order>(representation, Order.class);
jaxbRep.setContextPath("com.package.service.domain");

Order order = null;

try {

    order = jaxbRep.getObject();

}catch (IOException e) {
    ...
}

此我得到一个 java.io.IOException: Unable to unmarshal the XML representation.Unable to locate unmarshaller.jaxbRep.getObject()例外

因此,我还尝试了不同的方法,看看是否能取得的差,使用以下代码来代替:

@Override 
public void acceptRepresentation(Representation representation)
    throws ResourceException {

try{

    JAXBContext context = JAXBContext.newInstance(Order.class);

    Unmarshaller unmarshaller = context.createUnmarshaller();

    Order order = (Order) unmarshaller.unmarshal(representation.getStream());

} catch( UnmarshalException ue ) {
    ...
} catch( JAXBException je ) {
    ...
} catch( IOException ioe ) {
    ...
}

不过,这也给了我,当调用JAXBContext.newInstance由以下异常。

java.lang.NoClassDefFoundError: javax/xml/bind/annotation/AccessorOrder

预先感谢的任何建议。

有帮助吗?

解决方案

似乎还有一对夫妇的错误在这里,我从未有过的ObjectFactory类,我用了JAXB库的最新版本,加入这个类,并更新到2.1.11后,似乎现在的工作。

其他提示

在JAXB扩展的Restlet并没有为我工作了。我得到了相同的Unable to marshal异常多带些例外一起。奇怪的JAXBContext.newInstance()调用本身在我的代码工作得很好。正因为如此,我写了一个简单JaxbRepresenetation类:

public class JaxbRepresentation extends XmlRepresentation {

private String contextPath;
private Object object;

public JaxbRepresentation(Object o) {
    super(MediaType.TEXT_XML);
    this.contextPath = o.getClass().getPackage().getName();
    this.object = o;
}

@Override
public Object evaluate(String expression, QName returnType) throws Exception {
    final XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(this);

    return xpath.evaluate(expression, object, returnType);

}

@Override
public void write(OutputStream outputStream) throws IOException {
    try {
        JAXBContext ctx = JAXBContext.newInstance(contextPath);
        Marshaller marshaller = ctx.createMarshaller();
        marshaller.marshal(object, outputStream);
    } catch (JAXBException e) {
        Context.getCurrentLogger().log(Level.WARNING, "JAXB marshalling error!", e);
        throw new IOException(e);
    }
}
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top