Domanda

Ho creato uno schema XML annotando una classe modello di dominio Java esistente, ora quando cerco di usare JAXB per unmarshall la rappresentazione ha ricevuto nel mio webservice Restlet sto ottenendo una serie di errori non importa quello che mi sembra di provare . Sono nuovo di entrambe le restlets e JAXB così mi punta nella direzione di un esempio decente di utilizzare sia sarebbe utile un solo sono riuscito a trovare finora era qui: Esempio

I miei errori sono:

Se provo ad usare il 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) {
    ...
}

da questo ho un java.io.IOException: Unable to unmarshal the XML representation.Unable to locate unmarshaller. eccezione a jaxbRep.getObject()

Così ho anche provato un approccio diverso per vedere se questo ha fatto la differenza, utilizzando il seguente codice invece:

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

Tuttavia, questo mi dà anche la seguente eccezione quando chiamata a JAXBContext.newInstance è fatta.

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

Grazie in anticipo per qualsiasi consiglio.

È stato utile?

Soluzione

Sembra che ci fosse un paio di errori qui, non ho mai avuto una classe ObjectFactory e stavo usando fuori versioni aggiornate delle librerie JAXB, dopo l'aggiunta di questa classe e l'aggiornamento a 2.1.11 sembra funzionare ora

Altri suggerimenti

L'estensione JAXB per Restlet non ha funzionato anche per me. Ho avuto la stessa Unable to marshal Eccezione insieme ad alcune altre eccezioni. Stranamente il JAXBContext.newInstance() chiamarsi ha funzionato bene nel mio codice. A causa di ciò, ho scritto una semplice classe 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);
    }
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top