2017-08-10 91 views
0

我真的被來自SOAP服務的響應的解組問題困擾着。我使用springboot應用程序和WebServiceTemplate來調用現有的SOAP服務。我正在使用下面的代碼來設置用於編組和webservicetemplate的bean。任何幫助,高度讚賞。Springboot SOAP服務解組來自WebServiceTemplate的響應問題

打電話webServiceTemplate.marshalSendAndReceive(request);我期待TravelResponse object,但它給我JAXBElement<TravelResponse> object作爲迴應。我需要幫助理解
1)爲什麼是讓上面的響應,而不是TravelResponse 2)如何轉換爲TravelResponse

代碼片斷如下:

@Bean 
    Jaxb2Marshaller jaxb2Marshaller() { 
    Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller(); 
    jaxb2Marshaller.setContextPath("com.cater.trip.simple_api.trip.v1"); 
    return jaxb2Marshaller; 
    } 

    @Bean 
    public WebServiceTemplate webServiceTemplate() throws Exception { 
    WebServiceTemplate webServiceTemplate = new WebServiceTemplate(); 
    webServiceTemplate.setMessageFactory(getMessageFactory()); 
    webServiceTemplate.setMarshaller(jaxb2Marshaller()); 
    webServiceTemplate.setUnmarshaller(jaxb2Marshaller()); 
    webServiceTemplate.setDefaultUri(defaultUri); 
    webServiceTemplate.setMessageSender(getMessageSender()); 
    return webServiceTemplate; 
    } 

    @Bean 
    public SaajSoapMessageFactory getMessageFactory() { 
     return new SaajSoapMessageFactory(); 
    } 

    @Bean 
    public HttpComponentsMessageSender getMessageSender() {   
     return new HttpComponentsMessageSender(); 
    } 


@Override 
public Object getData() { 
    ObjectFactory clientFac = new ObjectFactory(); 
    TravelRequest request = populateRequest(clientFac);  
    TravelResponse res = (TravelResponse) webServiceTemplate.marshalSendAndReceive(request); 
    return res; 
} 

回答

1

按照Spring的文檔,WebServiceTemplate.marshalSendAndReceive( )返回非解組響應。

所以,你可以這樣做來返回預期的響應。 JAXBElement res =(JAXBElement)webServiceTemplate.marshalSendAndReceive(request);其中,JAXBElement res =(JAXBElement)webServiceTemplate.marshalSendAndReceive(request);

return res.getValue();