用于SOAP服务的Spring asyncRestTemplate

时间:2016-11-28 02:57:15

标签: spring web-services soap

我正在尝试使用Spring的AsyncRestTemplate调用soap服务。 我知道AsyncRestTemplate只支持休息调用,如果我们需要进行soap调用,那就是spring-ws。但Spring-ws不支持异步调用,并使用JDK的HttpURLConnection类进行http调用,我想调用异步soap webservices。 下面是我使用JDKs saaj api创建soapenvelop的代码。

public SOAPEnvelope createSoapEnvelope(Employee obj){
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage message = factory.createMessage();
    SOAPPart soapPart = message.getSOAPPart();
    try {
        SOAPEnvelope envelope = soapPart.getEnvelope();
        SOAPBody body = envelope.getBody();

        QName bodyName = new QName(Constants.SERVICE_NAMESPACE);
        SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.newDocument();

        // Marshal the Object to a Document
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.marshal(obj, document);
        body.addDocument(document);
        return envelope;
    }catch (SOAPException | JAXBException  | ParserConfigurationException e){
        LOGGER.error("Unable to marshal ",e);
    }
    return null;
}

我在HttpEntity中设置了这个soap包,通过下面的

等休息模板进行调用
MultiValueMap headers = new HttpHeaders();
    headers.set(HttpHeaders.ACCEPT,"text/xml;charset=UTF-8");
    headers.set(HttpHeaders.CONTENT_TYPE,"text/xml;charset=UTF-8");
    headers.set(HttpHeaders.ACCEPT_ENCODING,"gzip,deflate");
    headers.set("SOAPAction", Constants.SOAP_ACTION);
    HttpEntity<SOAPEnvelope> soapEntity = new HttpEntity<>(soapEnvelope,headers);
    ListenableFuture<ResponseEntity<SOAPEnvelope>> future=
            restTemplate.postForEntity(url,
   ,soapEntity,SOAPEnvelope.class);

执行此操作后,我得到例外org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [com.sun.xml.internal.messaging.saaj.soap.ver1_1.Envelope1_1Impl] and content type [text/xml;charset=UTF-8]

我从这个例外中了解到,Spring的HttpMessageConverters无法编组SoapEnvelop,这就是为什么这个客户端无法提交请求的原因。

我需要帮助编写自定义消息转换器并使用rest模板注册它。欢迎提出所有建议。

由于

0 个答案:

没有答案