2010-10-01 105 views
1

我一直在创建一个基于SAAJ的客户端。一切似乎都工作正常,直到我实现逻辑发送附件作为Web服务请求的一部分。SAAJ与JAXB的兼容性

Web服务操作很简单 - 它需要一个用于文件位置的字符串元素和一个用于文件内容的base64binary元素。

我已经测试了使用SoapUI的ws操作,并且一切似乎都是按顺序的。但是,当我从基于SAAJ的客户端发送文件附件时,Web服务操作将仅接收文件位置元素的值。我在ws-server上编写了一个处理程序来拦截WS操作请求,以查看附件是否能够访问Web服务。正如预期的那样,附件已经很好了,我可以使用处理程序中的SAAJ API来访问它的内容。

这只是让我想知道 - 使用SAAJ发送附件并通过JAXB绑定接收附件时是否存在兼容性问题?有什么我错过了吗?

感谢您的任何帮助!

回答

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[] 
      ... 
     } 

    } 

} 
+0

如何确保?另外,如果AttachmentUnmarshaller未注册,那么通过SoapUI发送的附件怎样才能正确解析? – anirvan 2010-10-01 12:57:20

+0

您是否使用生成的客户端或手写客户端与您的服务? – 2010-10-01 13:26:33

+0

客户是手写的,基于SAAJ。由于客户端需要与许多Web服务进行接口,因此我们不能简单地生成特定于任何特定WSDL的绑定。 – anirvan 2010-10-01 13:28:25