XML Unmarshalling باستخدام JAXB في طريقة acceptRepresentation restlet ل

StackOverflow https://stackoverflow.com/questions/1061001

سؤال

ولقد خلق مخطط XML التي كتبها التأشير فئة نموذج المجال جافا القائمة، والآن عندما أحاول استخدام JAXB إلى unmarshall تمثيل تلقى داخل بلدي خدمة ويب restlet أنا الحصول على مجموعة من الأخطاء بغض النظر عن ما يبدو لي أن محاولة . أنا جديدة على حد سواء restlets وJAXB ذلك مشيرا لي في اتجاه مثال لائق باستخدام كل من شأنها أن تكون مفيدة واحدة فقط لقد تمكنت من العثور على وحتى الآن هنا: <لأ href = "HTTP: //sleeplessinslc.blogspot كوم / 2008/04 / rest.html "يختلط =" نوفولو noreferrer "> مثال

وبلدي الأخطاء:

إذا وأنا أحاول أن استخدام JaxbRepresentation restlet.ext.jaxb:

@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