在restlet';中使用JAXB对XML进行解组;s表示法
我通过注释一个现有的Java域模型类创建了一个XML模式,现在当我尝试使用JAXB来解组restlet Web服务中接收到的表示时,无论我尝试了什么,都会遇到大量错误。我对restlets和JAXB都是新手,因此为我指出一个使用这两种技术的好例子会很有帮助,到目前为止,我找到的唯一一个例子是: 我的错误是: 如果我尝试使用restlet.ext.jaxb jaxbre演示文稿:在restlet';中使用JAXB对XML进行解组;s表示法,xml,jaxb,restlet,unmarshalling,Xml,Jaxb,Restlet,Unmarshalling,我通过注释一个现有的Java域模型类创建了一个XML模式,现在当我尝试使用JAXB来解组restlet Web服务中接收到的表示时,无论我尝试了什么,都会遇到大量错误。我对restlets和JAXB都是新手,因此为我指出一个使用这两种技术的好例子会很有帮助,到目前为止,我找到的唯一一个例子是: 我的错误是: 如果我尝试使用restlet.ext.jaxb jaxbre演示文稿: @Override public void acceptRepresentation(Representation
@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) {
...
}
但是,在调用JAXBContext.newInstance时,这也给了我以下异常
java.lang.NoClassDefFoundError: javax/xml/bind/annotation/AccessorOrder
提前感谢您的建议。这里似乎有几个错误,我从来没有ObjectFactory类,我使用的是过时版本的JAXB库,在添加这个类并更新到2.1.11之后,它现在似乎可以工作了。Restlet的JAXB扩展对我来说也不起作用。我得到了相同的
无法封送
异常以及一些其他异常。奇怪的是,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);
}
}
}
为每个write()实例化封送拆收器的成本非常高。Restlet缓存实例化,以便只发生一次。我认为问题可能在于Restlet绑定到一个独立的jaxbjar,而不是使用JRE中捆绑的实现。我将调查这个问题,因为它会导致应用程序引擎出现问题。
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);
}
}
}