【问题标题】:java - Set content type on a soap servicejava - 在肥皂服务上设置内容类型
【发布时间】:2016-09-13 07:54:57
【问题描述】:

有人可以告诉我如何在 java 中设置soap 服务的内容类型吗?我想将内容类型设置为“multipart/related”。我搜索了很多问题,但我无法弄清楚我该怎么做。

我有这样的事情:

代理类:

@WebService(name = "DocumentManagementForUnderwritingService",targetNamespace = "myNameSpace")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@XmlSeeAlso({
   //some other classes
})
public interface DocumentManagementForUnderwritingService {

  @WebMethod
    @WebResult(name = "uploadDocumentResponse", targetNamespace = "myNameSpace", partName = "Body")
    public UploadDocumentResponse uploadDocument(@WebParam(name = "uploadDocumentRequest", targetNamespace = ""myNameSpace", partName = "Body")
    UploadDocumentRequest body) throws ServiceException, SystemException ;

}

请求类

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "uploadDocumentRequest", propOrder = {
    "positionId",
    "username",
    "documentItemId",
    "documentCod"
    })
    public class UploadDocumentRequest {

    @XmlElement(required = true)
    protected String positionId;
    @XmlElement(required = true)
    protected String username;
    protected String documentItemId;
    protected String documentCod;

    //setters & getters 
}

在调用服务的类中(我认为在这里我必须以某种方式设置内容类型)

 BindingProvider bp = (BindingProvider) proxy;
  UploadDocumentRequest request = new UploadDocumentRequest();
  request.setDocumentItemId(input.getDocumentItemId());
  request.setPositionId(input.getPositionId());

UploadDocumentResponse response = proxy.uploadDocument(request);

我还链接了一个处理程序,我试图在其中设置 Mime_type 并添加附件:

private Static final String MULTIPART_MYME_TYPE="multipart/related";

 @Override
    public boolean handleMessage(SOAPMessageContext context) {
        Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

        try {

            if (isRequest) {

                if (context.containsKey("pdf")) {
                    byte[] arr = (byte[]) context.get("pdf");

                    SOAPMessage soapMsg = context.getMessage();
                    soapMsg.getMimeHeaders().addHeader("Content-type", MULTIPART_MIME_TYPE);
                    AttachmentPart attachment = createAttachment(soapMsg, arr, "test.pdf", context);
                    soapMsg.addAttachmentPart(attachment);
                    Iterator<AttachmentPart> it = context.getMessage().getAttachments();
                    while (it.hasNext()) {
                        AttachmentPart att = it.next();
                        System.out.println(att.getContent());
                    }
                    System.out.println("ok");
                }
            }
        } catch (Exception e) {
            return false;
        }

        return true;

    }


 private AttachmentPart createAttachment(SOAPMessage msg, byte[] payload, String fileId, SOAPMessageContext context) {

        @SuppressWarnings("unchecked")
        Map<String, DataHandler> attachmentsMap = (Map<String, DataHandler>) context.get(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS);


        ByteArrayDataSource ds = new ByteArrayDataSource(payload, MULTIPART_MIME_TYPE);
        DataHandler dh = new DataHandler(ds);

        AttachmentPart attachmentPart = msg.createAttachmentPart();

        attachmentPart.setContent(new ByteArrayInputStream(payload), MULTIPART_MIME_TYPE);
        attachmentPart.setContentId(fileId);

        String contentDisposition = "Content-Disposition: attachment; name=\"" + fileId + "\"";
        attachmentPart.addMimeHeader("Content-Disposition", contentDisposition);

        msg.addAttachmentPart(attachmentPart);

        attachmentsMap.put(fileId, dh);

        context.put(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS, attachmentsMap);

        context.getMessage().getAttachments();

        return attachmentPart;
    }

提前谢谢你!

【问题讨论】:

  • 您使用的是哪个 WS 实现?我不认为它可以通过 JEE api 访问 - 看这里stackoverflow.com/questions/2372336/…
  • 不幸的是我已经看到了这个问题。我尝试使用“Content-Type”和“Content-type”,但仍然遇到同样的问题
  • 好吧,那么也许您可以尝试将 HandlerChain 附加到 WS,但我不确定它是否也可以在客户端模式下工作(也许应该?)或者可能更简单的方法是使用一些代理服务器来重写它标题。
  • 我也这样做了。我编辑了我的问题。请在上面找到它。

标签: java web-services soap


【解决方案1】:

将绑定强制转换为 SOAPBinding,并且有一个启用 MTOM 的标志。

import javax.xml.ws.soap.SOAPBinding;

BindingProvider bp = (BindingProvider) proxy;

// Set binding and MTOM
SOAPBinding binding = (SOAPBinding) bp.getBinding();
binding.setMTOMEnabled(true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 2012-06-28
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多