Pergunta

Eu criei um esquema XML anotando uma classe de modelo de domínio Java existente, agora quando eu tento usar JAXB para desempacotar a representação recebida dentro do meu webservice Restlet que estou recebendo uma série de erros, não importa o que eu parecem tentar . Eu sou novo para ambos os Restlets e JAXB tão apontando-me na direção de um exemplo digno de usar tanto seria útil único que eu consegui encontrar até agora foi aqui: Exemplo

Os meus erros são:

Se eu tentar usar o 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) {
    ...
}

A partir disso, obter um java.io.IOException: Unable to unmarshal the XML representation.Unable to locate unmarshaller. exceção em jaxbRep.getObject()

Então, eu também tentou uma abordagem diferente para ver se isso fez a diferença, usando o seguinte código em vez disso:

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

No entanto, isto também me dá a seguinte exceção quando chamada para JAXBContext.newInstance é feita.

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

Agradecemos antecipadamente por qualquer conselho.

Foi útil?

Solução

Parece que havia um par de erros aqui, eu nunca tive uma classe ObjectFactory e eu estava usando fora de versões atualizadas das bibliotecas JAXB, depois de adicionar esta classe e atualização para 2.1.11 parece funcionar agora

Outras dicas

A extensão JAXB para Restlet não funcionou para mim também. Eu tenho a mesma exceção Unable to marshal juntamente com mais algumas exceções. Estranhamente o próprio chamada JAXBContext.newInstance() funcionou bem no meu código. Por causa disso, eu escrevi uma classe JaxbRepresenetation simples:

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);
    }
}
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top