2016-04-06 96 views
0

我已經在JAX-WS中設置了一個SOAP WebServiceProvider,但是我很難弄清楚如何從SOAP請求和響應中獲取XML格式的請求和響應。下面是我得到了代碼示例,現在,在那裏我試圖抓住XML:如何轉換XML格式的SOAP請求和響應?

  package com.ewb.socialbanking.creditcardMain; 
      import org.springframework.ws.client.core.support.WebServiceGatewaySupport; 
      import org.springframework.ws.soap.client.core.SoapActionCallback; 
      import com.ewb.socialbanking.creditcardws.GetCcNumber; 
      import com.ewb.socialbanking.creditcardws.GetCcNumberResponse; 
      import com.safenet.wsdl.LoginUser; 

      /*THIS IS HOW I AM GIVING THE REQUEST : 
     AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); 
       ctx.register(CreditCardConfig.class); 
      ctx.refresh(); 

      CreditCardClient cCClient = ctx.getBean(CreditCardClient.class); 
      GetCcNumber cCNumber = new GetCcNumber(); 

    ObjectFactory enrollObjFactory = new ObjectFactory(); 

    cCNumber.setT24Cif(enrollObjFactory.createString("abc")); 
    cCNumber.setLinkId(enrollObjFactory.createString("def")); 
    cCNumber.setCcCif(enrollObjFactory.createString("ghi")); 
    cCNumber.setMsgRefNo(enrollObjFactory.createString("jkl")); 

    GetCcNumberResponse valueForRes = cCClient.getCreditCardDetails(cCNumber);*/ 



     public class CreditCardClient extends WebServiceGatewaySupport { 
     public GetCcNumberResponse getCreditCardDetails(GetCcNumber request) { 
     //I want here request in xml format?? 
     System.out.println("req : "+request); 
     //Right now it is coming as : 
     //req : [email protected] 
     GetCcNumberResponse response = null; 
    try { 
     response = (GetCcNumberResponse) getWebServiceTemplate() 
       .marshalSendAndReceive(
         request, 
         new SoapActionCallback(
           "http://F9M9MV1RENTAL:8088/mocksoap/GetCcNumber")); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    //I want here response in xml format?? 
    System.out.println("res : "+response); 
     //Right now it is coming as : 
     //res : [email protected] 
    return response; 
} 

}

+0

您需要將對象序列化爲字符串。 – Rao

+0

沒有它的不工作:( – Asad

+0

請有人幫助....它的非常迫切的要求....請讓我知道,如果你想問一些東西!! – Asad

回答

0

我已經通過我自己試過,和它的作品。如果你想獲得SOAP消息,一個好辦法是在服務器端使用一個處理程序。以下是我的處理程序。

package com.documentType.handler; 

import java.io.IOException; 
import java.util.Set; 

import javax.xml.namespace.QName; 
import javax.xml.soap.SOAPException; 
import javax.xml.ws.handler.MessageContext; 
import javax.xml.ws.handler.soap.SOAPHandler; 
import javax.xml.ws.handler.soap.SOAPMessageContext; 

public class TestHandler implements SOAPHandler<SOAPMessageContext> { 

    @Override 
    public void close(MessageContext arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public boolean handleFault(SOAPMessageContext arg0) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    // this method will be called twice (in and out) 
    @Override 
    public boolean handleMessage(SOAPMessageContext context) { 

     // true if the msg is going out 
     Boolean outBoundMsg = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); 

     try { 
      if (outBoundMsg) { 
       System.out.println("this is response"); 
       context.getMessage().writeTo(System.out); 
      } else { 
       System.out.println("this is request"); 
       context.getMessage().writeTo(System.out); 
      } 

     } catch (SOAPException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return true; 
    } 

    @Override 
    public Set<QName> getHeaders() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

} 

在控制檯輸出結果如下

this is request 
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Header/><S:Body><ns2:echo xmlns:ns2="http://ws.documentType.com/"><arg0>yoyoyo</arg0></ns2:echo></S:Body></S:Envelope> 
this is response 
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:echoResponse xmlns:ns2="http://ws.documentType.com/"><return>echo: yoyoyo</return></ns2:echoResponse></S:Body></S:Envelope> 

如果你有困難添加處理程序,請按照下面的教程

http://www.mkyong.com/webservices/jax-ws/jax-ws-soap-handler-in-server-side/

http://www.mkyong.com/webservices/jax-ws/jax-ws-soap-handler-in-client-side/

http://www.mkyong.com/webservices/jax-ws/jax-ws-soap-handler-testing-for-client-and-server-side/

0

JAX-WS服務返回JAXB對象。如果您想將該對象封裝到outputstream中,則只需使用JAXB API。

Marshaller m = JAXBContext.newInstance(GetCcNumberResponse.class).createMarshaller(); 
m.marshal(response, System.out);