如何以XML格式转换SOAP请求和响应?

时间:2016-04-06 11:38:15

标签: web-services soap cxf jax-ws soapui

我在JAX-WS中设置了一个SOAP WebServiceProvider,但是我无法弄清楚如何从SOAP请求和响应中获取XML格式的请求和响应。以下是我现在获得的代码示例,以及我试图获取XML的地方:

          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 : com.ewb.socialbanking.creditcardws.GetCcNumber@5d534f5d
       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 : com.ewb.socialbanking.creditcardws.GetCcNumberResponse@514646ef
    return response;
}

}

2 个答案:

答案 0 :(得分: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/

答案 1 :(得分:0)

JAX-WS服务返回JAXB对象。如果要将该对象编组为输出流,只需使用JAXB API。

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