RestletのacceptRepresentation方法でJAXBを使用してアンマーシャリングXML
-
21-08-2019 - |
質問
私は関係なく、私がしようとしているように見える何のエラーのホストを取得していないよ、私のRestlet Webサービス内で受信今、既存のJavaドメインモデルクラスに注釈を付けることにより、XMLスキーマを作成しました。私はrestletsとJAXBの両方に新たなんだので、両方を使用してのまともな例の方向に私を指していることは、私がこれまでに見つけることができた唯一の参考になるここにあった:<のhref = "のhttp://sleeplessinslc.blogspot .COM / 2008/04 / rest.html」のrel = "nofollowをnoreferrer">例の
私のエラーがあります:
私は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にこのクラスと更新を追加した後、今動作しているようです。
他のヒント
の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);
}
}
}