0

I've set up a SOAP WebServiceProvider in JAX-WS, but I'm having trouble figuring out how to get request and response in XML format from a SOAP request and response. Here's a sample of the code I've got right now, and where I'm trying to grab the 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;
}

}

7
  • You need to serialize the object to string. Commented Apr 6, 2016 at 11:56
  • no its not working :( Commented Apr 6, 2016 at 12:16
  • Please somebody help....its very urgent requirement....please let me know if u want to ask something !! Commented Apr 6, 2016 at 12:32
  • What you mean by not working? Can you please show what you tried? Commented Apr 6, 2016 at 12:38
  • Please check if this helps Commented Apr 6, 2016 at 12:38

2 Answers 2

1

JAX-WS services return JAXB objects. If you want to marshall that object to an outputstream, you simply use the JAXB API.

Marshaller m = JAXBContext.newInstance(GetCcNumberResponse.class).createMarshaller();
m.marshal(response, System.out);
Sign up to request clarification or add additional context in comments.

1 Comment

Works only if GetCcNumberResponse class has an @XmlRootElement annotation. codenotfound.com/…
0

I've tried by my self, and it works. If you want to get the SOAP message, a good way to do it is using a handler at server side. The following is my handler.

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;
    }

}

The output in console is as the following

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>

If you have difficulty to add a handler, follow the following tutorial

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.