2009-06-29 10 views
0

既存のJavaドメインモデルクラスに注釈をつけてXMLスキーマを作成しました。これは、JAXBを使用してレシートWebサービスで受け取った表現をアンマーシャリングしようとしたときです。私が何をしようとしているかにかかわらずエラー。私は両方のrestletsに新たなんだとJAXBはとても参考になるの両方を使用してのまともな例の方向に私を指している唯一の私は、これまでここにいた見つけることができた:ExamplerestletのacceptRepresentationメソッドでJAXBを使用したXMLのアンマーシャリング

私のエラーは、次のとおりです。

私は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) { 
    ... 
} 

を私はjaxbRep.getObject()

java.io.IOException: Unable to unmarshal the XML representation.Unable to locate unmarshaller. 例外を取得だから私ALS Oの代わりに次のコードを使用して、それが違いを作ったかどうかを確認するために異なるアプローチを試してみました:

@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 

アドバイスを事前にいただきありがとうございます。

答えて

0

は、エラーのカップルがここにあったようだが、私はのObjectFactoryクラスを持っていなかったと私はJAXBライブラリの日付のバージョンのうち、使用していた、2.1.11にこのクラスを追加して更新した後、

0
今動作しているようです

RestletのJaxb拡張機能も私のためには機能しませんでした。私は同じ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); 
    } 
} 
} 
+0

をすべての書込みのためのマーシャラーのインスタンスを作成する()は非常に高価です。 Restletはインスタンシエーションをキャッシュするので、一回しか起こりません。私は、RestletがJREにバンドルされている実装を使用する代わりに、スタンドアロンのjaxb jarにバインドするという問題があると思います。 App Engineで問題が発生するため、この問題を調査します。 – ZiglioUK

関連する問題