我一直致力于创建一个基于SAAJ的客户端。一切似乎都运行良好,直到我实现了将附件作为Web服务请求的一部分发送的逻辑。
Web服务操作很简单 - 它需要一个用于文件位置的字符串元素,以及一个用于文件内容的base64binary元素。
我已经使用SoapUI测试了ws操作,一切似乎都是有序的。但是,当我从基于SAAJ的客户端发送文件附件时,Web服务操作只会收到文件位置元素的值。我在ws-server上编写了一个处理程序来拦截WS操作请求,以查看附件是否到达Web服务。正如所料,附件达到了正常,我可以使用处理程序中的SAAJ api访问其内容。
这只是让我想知道 - 使用SAAJ发送附件并通过JAXB绑定接收它们时是否存在兼容性问题?有什么我错过了吗?
感谢您的帮助!
答案 0 :(得分:1)
您需要确保在Unmarshaller上注册了AttachmentUnmarshaller才能在JAXB中接收附件。
import javax.activation.DataHandler;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.attachment.AttachmentUnmarshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jaxbContext = JAXBContext.newInstance(Demo.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setAttachmentUnmarshaller(new MyAttachmentUnmarshaller());
}
private static class MyAttachmentUnmarshaller extends AttachmentUnmarshaller {
@Override
public DataHandler getAttachmentAsDataHandler(String cid) {
// TODO - Lookup MIME content by content-id, cid, and return as a DataHandler.
...
}
@Override
public byte[] getAttachmentAsByteArray(String cid) {
// TODO - Retrieve the attachment identified by content-id, cid, as a byte[]
...
}
}
}