2014-11-14 26 views
0

我們如何通過基於Rest的項目使用Spring Maven來訪問其戰爭部署在同一臺服務器上的soap項目的wsdl。基本上,我必須訪問通過wsdl公開的API,並且我必須訪問此API,響應比需要從其他POST方法作爲JSON返回的響應。它將像REST post方法一樣,接受輸入並調用此API(來自wsdl)並將響應操作爲JSON,但我不得不在不知情的情況下跳入WebServices和Spring框架。所以,任何幫助或指示,以快速學習這些東西將不勝感激。在基於Spring REST的客戶端訪問通過wsdl公開的API

回答

-1
Hi I have used the following approach to implement the above requirement: 
http://myshittycode.com/2013/10/01/using-spring-web-services-and-jaxb-to-invoke-web-service-based-on-wsdl/ 
1. changed the pom to add spring-ws dependency and plugin. 
2. build the classes and it generated the classes from the wsdl. 
3. changed the application xml : 
    <!--Generating web sources--> 

    <!-- Define the SOAP version used by the WSDL --> 
    <bean id="soapMessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"> 
     <property name="soapVersion"> 
      <util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12"/> 
     </property> 
    </bean> 

    <!-- The location of the generated Java files --> 
    <oxm:jaxb2-marshaller id="marshaller" contextPath="com.pb.pims.generatedsources"/> 

    <!-- Configure Spring Web Services --> 
    <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate"> 
     <constructor-arg ref="soapMessageFactory"/> 
     <property name="marshaller" ref="marshaller"/> 
     <property name="unmarshaller" ref="marshaller"/> 
     <property name="defaultUri" value="http://localhost/HSWS/services/HSService?wsdl"/> 
    </bean> 

4. Created the Service class; 

@Service 
public class HSService { 
    @Autowired 
    private WebServiceTemplate webServiceTemplate; 


    public List<HSChild> getHSChildren(String hscode, String country,String limit) { 

     GetHSChildren getHSChildren= new ObjectFactory().createGetHSChildren(); 

     getHSChildren.setCountry(country); 
     getHSChildren.setHsCode(hscode); 
     getHSChildren.setLimit(Integer.parseInt(limit)); 

     GetHSChildrenResponse response = (GetHSChildrenResponse) webServiceTemplate.marshalSendAndReceive(getHSChildren); 

     return response.getGetHSChildrenReturn(); 
    } 


    public static void main(String[] args) { 
     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
     HSService hsService = context.getBean(HSService.class); 
    } 
} 


So, I am able to call this aPI from the wsdl via my client. But I am always getting the values of the getGetHSChildrenReturn. hscode and getGetHSChildrenReturn.description as null. 

Please find below the getGetHSChildrenReturn.class generated in the Step1 : 


@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "getHSChildrenReturn" 
}) 
@XmlRootElement(name = "getHSChildrenResponse") 
public class GetHSChildrenResponse { 

    @XmlElement(required = true) 
    protected List<HSChild> getHSChildrenReturn; 

    public List<HSChild> getGetHSChildrenReturn() { 
     if (getHSChildrenReturn == null) { 
      getHSChildrenReturn = new ArrayList<HSChild>(); 
     } 
     return this.getHSChildrenReturn; 
    } 

Also, I verified in the service code , which we are invoking via this wsdl by putting logging, that the correct request is going and it is returning the expected response at service end. But while coming to the client, the values are set as null. 

Please help, what's wrong here in the client side. 

Thanks in advance. 
0

你需要做到以下幾點:Spring - Consuming a SOAP service

  1. 從WSDL
    這可以在Spring中與下述技術acomplished創建客戶端代碼。它將生成您需要從REST API代碼中調用該服務的Java類。在這種情況下,您在同一臺服務器上調用另一項服務,您只需將端點url設置爲您的服務器即可。
  2. 創建您的REST API
    您可以使用Spring MVC設計您的REST API並調用SOAP服務。您需要開發一個帶有不同端點和正確的請求和響應對象的Controller類。 Spring MVC會使用Jackson框架自動將這些請求和響應對象轉換爲JSON。使用本指南:Building a RESTful Web Service

這是從Java REST API消耗SOAP服務的通用方式。如果目標是簡單地將SOAP服務作爲REST服務公開,那麼您可以返回從WSDL生成的響應對象。如果這是一個選項,我會仔細考慮重構SOAP服務代碼並將其作爲REST API公開。

注意:通過直接使用JAX-WS實現消耗SOAP的美好時光,並通過Jackson公開JSON對象。