有人可以告诉我如何在java中设置soap服务的内容类型吗?我想将内容类型设置为“多部分/相关”。我搜索了很多问题,但我不知道该怎么做。
我有这样的东西:
代理类:
@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;
}
提前谢谢您!
最佳答案
将绑定(bind)转换为 SOAPBinding,并且有一个启用 MTOM 的标志。
import javax.xml.ws.soap.SOAPBinding;
BindingProvider bp = (BindingProvider) proxy;
// Set binding and MTOM
SOAPBinding binding = (SOAPBinding) bp.getBinding();
binding.setMTOMEnabled(true);
关于java - 设置 SOAP 服务的内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39464874/