首页 > 解决方案 > Spring ws - Swaref 的数据处理程序仍然为空

问题描述

我使用 Spring Boot 启动器 Web 服务开发了一个带有附件服务的 SOAP。

由于未知原因,附件未解组。使用了 Jaxb Unmarshaller,但内部的属性 AttachmentUnmarshaller 为“null” ...所以可能是 DataHandler 解组未完成的原因?

与在 JEE 环境中一样,附件Unmarshaller 由 jaxws 处理。如何在独立进程中配置它,例如在 tomcat 上的 spring boot ?

Java 版本:8_0_191

春季启动版本:2.1

标签: springspring-bootspring-ws

解决方案


我遇到了类似的问题,但有编组。

Jaxb2Marshaller 有自己的AttachmentMarshaller和实现AttachmentUnarshaller。但是为了使这些工作,mtomEnabled属性应该设置为 true。如果不是,将使用未实例化的默认值。

尝试setMtomEnabled(true)在您的 Jaxb2Marshaller 上进行设置。这可能会解决您的问题。

对于遇到相同编组问题的人来说 - 它有点复杂。Jaxb2AttachmentMarshaller没有按照 WS-I Attachment Profile 1.0 正确实现 - http://www.ws-i.org/Profiles/AttachmentsProfile-1.0.html#Example_Attachment_Description_Using_swaRef

您将不得不覆盖Jaxb2Marshaller当时的编组行为。

注意:此解决方案假定 MTOM 始终处于禁用状态。

@Configuration
class SOAPConfiguration {
    @Bean
    public Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller() {
            @Override
            public void marshal(Object graph, Result result, @Nullable MimeContainer mimeContainer) throws XmlMappingException {
                try {
                    javax.xml.bind.Marshaller marshaller = createMarshaller();
                    if (mimeContainer != null) {
                        marshaller.setAttachmentMarshaller(
                                new SwaRefAttachmentMarshaller(mimeContainer)
                        );
                        marshaller.marshal(graph, result);
                    } else {
                        super.marshal(graph, result, null);
                    }
                } catch (JAXBException ex) {
                    throw convertJaxbException(ex);
                }
            }
        };
        marshaller.setPackagesToScan("my.package");

        marshaller.setMtomEnabled(false);

        return marshaller;
    }

    private class SwaRefAttachmentMarshaller extends AttachmentMarshaller {

        private final MimeContainer mimeContainer;

        private SwaRefAttachmentMarshaller(MimeContainer mimeContainer) {
            this.mimeContainer = mimeContainer;
        }

        @Override
        public String addMtomAttachment(DataHandler data, String elementNamespace, String elementLocalName) {
            return null;
        }

        @Override
        public String addMtomAttachment(byte[] data, int offset, int length, String mimeType, String elementNamespace, String elementLocalName) {
            return null;
        }

        @Override
        public String addSwaRefAttachment(DataHandler data) {
            String attachmentId = UUID.randomUUID().toString();
            mimeContainer.addAttachment("<" + attachmentId + ">", data);

            return "cid:" + attachmentId;
        }
    }
}


推荐阅读