Restlet의 Acceptrepresentation 메소드에서 JAXB를 사용하여 XML을 방해하지 않습니다

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

문제

기존 Java 도메인 모델 클래스에 주석을 달아 XML 스키마를 만들었습니다. 이제 JAXB를 사용하여 Restlet WebService 내에서받은 표현을 해소하려고 할 때 시도하는 것처럼 보이든 많은 오류가 발생합니다. 나는 레스토리와 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) {
    ...
}

이것으로부터 나는 a를 얻는다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

조언에 미리 감사드립니다.

도움이 되었습니까?

해결책

여기에는 몇 가지 오류가 있었는데, 나는 객체 요건적 인 클래스가 없었고,이 클래스를 추가하고 2.1.11로 업데이트 한 후에는 JaxB 라이브러리의 날짜가 아닌 버전을 사용하고있었습니다.

다른 팁

Restlet의 JAXB 확장도 나에게도 효과가 없었습니다. 나도 똑같아 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