【问题标题】:spring ws soap mtom request error - Cannot create message: incorrect content-type for SOAP version. Got multipart/related; but expected text/xmlspring ws soap mtom 请求错误 - 无法创建消息:SOAP 版本的内容类型不正确。得到多部分/相关;但预期的文本/xml
【发布时间】:2022-07-03 18:06:03
【问题描述】:

我正在尝试发送一个启用 mtom 的soap请求,该请求在通过 SoapUI 调用时工作正常,但在使用 spring ws api 发送时会出现以下错误-

SEVERE: SAAJ0533: Cannot create message: incorrect content-type for SOAP version. Got multipart/related; boundary="----=_Part_0_1559020039.1653221075665"; 
    type="application/soap+xml"; start-info="text/xml", but expected text/xml


org.springframework.ws.soap.SoapMessageCreationException: Could not create message from InputStream: Unable to internalize message; nested exception is com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to internalize message

这是 Soap 客户端的代码:-

FileUploadRequestType fileUploadRequestType = this.objectFactory.createFileUploadRequestType();

        fileUploadRequestType.setFileType(FileTypeType.TXT);
        fileUploadRequestType.setFileName("Transactions_20200424_80.txt");

        fileUploadRequestType.setAttachmentID(new DataHandler(
                Thread.currentThread().getContextClassLoader()
                        .getResource("Transactions_20200424_80.txt")));

        JAXBElement<FileUploadRequestType> fileRequestJAXBElement
                = objectFactory.createFileUploadRequest(fileUploadRequestType);

        WebServiceMessageCallback webServiceMessageCallback = (WebServiceMessage message) -> {

            SoapHeader soapHeader = ((SoapMessage) message).getSoapHeader();
            RequestContextType requestContextType = objectFactory.createRequestContextType();
            requestContextType.setRequestID("c09e319d-8e9f-4a32-9226-0df9f9bf3601");
            requestContextType.setServiceName(SrvcNameType.LYREPORT);
            requestContextType.setRequestChannel(ChannelType.SYSTEM);
            requestContextType.setMessageTime(LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS).toString());
            requestContextType.setPartner(PartnerType.ALPHA);
            JAXBElement<RequestContextType> headerJAXBElement = objectFactory.createRequestContext(requestContextType);

            // create a marshaller
            JAXBContext context = null;
            Marshaller marshaller = null;
            try {
                context = JAXBContext.newInstance(RequestContextType.class);
                marshaller = context.createMarshaller();
                marshaller.marshal(headerJAXBElement, soapHeader.getResult());
            } catch (JAXBException e) {
                System.out.println("Error while marshalling headers.");
                e.printStackTrace();
            }
        };

        ResponseContextType responseContextType = null;
        try {
            getWebServiceTemplate().marshalSendAndReceive(fileRequestJAXBElement, webServiceMessageCallback);
            responseContextType = MtomClientConfig.responseContextType;

        } catch (WebServiceIOException e) {
            System.out.println("Could not connect to the soap web service.");
            throw new RuntimeException(e.getLocalizedMessage());
        } 

这是配置:-

@Configuration
public class MtomClientConfig {

    public static ResponseContextType responseContextType = new ResponseContextType();

    @Bean
    public Jaxb2Marshaller marshaller() {

        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("org.springframework.ws.samples.mtom.client.sws");
        marshaller.setMtomEnabled(true);
        return marshaller;
    }

    @Bean
    public SaajSoapMessageFactory saajSoapMessageFactory() {
        SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory();
        return messageFactory;
    }


    @Bean
    public SaajMtomClient saajClient(Jaxb2Marshaller marshaller, SaajSoapMessageFactory saajSoapMessageFactory ) throws SOAPException {

        WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
        HttpsUrlConnectionMessageSender messageSender = new HttpsUrlConnectionMessageSender();
        messageSender.setTrustManagers(new TrustManager[]{new UnTrustworthyTrustManager()});

        webServiceTemplate.setMessageSender(messageSender);
        webServiceTemplate.setMessageFactory(saajSoapMessageFactory);
        
        SaajMtomClient client = new SaajMtomClient();
        client.setWebServiceTemplate(webServiceTemplate);
        client.setDefaultUri("https://host:port/GW/AlphaServices/FileUpload");
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    }
}

我还尝试通过配置以下 MessageFactories 来设置 SOAP 版本 1.2,但随后出现以下错误:-

SEVERE: SAAJ0415: InputStream does not represent a valid SOAP 1.2 Message

org.springframework.ws.soap.SoapMessageCreationException: Could not create message from InputStream: InputStream does not represent a valid SOAP 1.2 Message; nested exception is javax.xml.soap.SOAPException: InputStream does not represent a valid SOAP 1.2 Message

MessageFactory 设置 SOAP 版本 1.2:-

    @Bean
    public SaajSoapMessageFactory saajSoapMessageFactory() {
        SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory();
        messageFactory.setSoapVersion(SoapVersion.SOAP_12);
        return messageFactory;
    }

//    @Bean
//    public WebServiceMessageFactory webServiceMessageFactory() throws SOAPException {
//        SaajSoapMessageFactory saajSoapMessageFactory = new SaajSoapMessageFactory();
//        saajSoapMessageFactory.setSoapVersion(SoapVersion.SOAP_12);
//        return saajSoapMessageFactory;
//    }

另外一点我认为我应该补充的问题是,当我尝试发送不带标头部分的请求时(注释了 WebServiceMessageCallback 中的代码),它确实有效,但显然收到了来自 web 服务的错​​误响应,但是这里的重点是它实际上在没有标题的情况下工作可能是我们需要查看的地方。

如果我应该在问题中添加更多信息,请告诉我。

【问题讨论】:

    标签: java soap soap-client spring-ws mtom


    【解决方案1】:

    好的,经过长时间的调试,终于集成成功了。但是我没有更改代码中的一个字母。似乎服务提供商在他们的最后改变了一些东西,他们在询问时没有确认。我知道它像什么??你是认真的??我只是想调试一个一直都很好的代码?? 常见...我知道它令人失望..只是想发布答案,以便面临类似问题且已经调试所有可能方面的人应该检查非常基本的 - 1)如果他们使用更新和正确的 wsdl 文件 2)确保服务提供者/消费者拥有正确的配置集。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多