我已经创建了一个XML模式,通过注释现有的Java域模型类,现在当我尝试使用JAXB解组在我的restlet webservice中收到的表示时我得到错误无论我似乎尝试。我是新来的都restlets和JAXB这样指着我同时使用将是有益的体面的例子的方向只有一个,我设法找到至今在这里:Example在restlet的acceptRepresentation方法中使用JAXB解组XML使用JAXB
我的错误是:
如果我尝试使用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) {
...
}
从此我在jaxbRep.getObject()
得到 java.io.IOException: Unable to unmarshal the XML representation.Unable to locate unmarshaller.
例外,所以我阿尔斯Ø尝试了不同的方法,看看是否能带来差异,使用下面的代码来代替:
@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
在此先感谢您的任何建议。
为每个write()实例化一个编组器非常昂贵。 Restlet缓存实例化,只发生一次。我认为问题可能是Restlet绑定到独立的jaxb jar而不是使用JRE中捆绑的实现。我要调查此问题,因为它会在App Engine上导致问题。 – ZiglioUK 2012-08-09 12:51:29