0

既存の 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

アドバイスをよろしくお願いします。

4

2 に答える 2

0

Restlet の Jaxb 拡張機能も機能しませんでした。同じUnable to marshal例外と、さらにいくつかの例外が発生しました。奇妙なことに、JAXBContext.newInstance()呼び出し自体は私のコードでうまくいきました。そのため、単純な JaxbRepresenation クラスを作成しました。

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);
    }
}
}
于 2010-04-16T11:47:03.777 に答える