Remove element in JAX-WS SOAP Response

2020-07-20 05:25发布

问题:

I'm having the following JAX-WS method

public School createUpdateSchool(Perks reqeustData, DataHandler contentData)
{
----
----
}

And in my SOAP response,

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns3:CreateUpdateSchoolResponse xmlns:ns2=".........">
<return>
<ns2:Student name="Raj" ......
<ns2:Exam pattern="CBSE"............       
............    
</return>
</ns3:CreateUpdateSchoolResponse>
</S:Body>
</S:Envelope> 

In fact I can able to customize the element but i'm unable to remove it altogether from the soap response.

  • Is it possible to remove this by some configuration?
  • if not, how to do this at least by SOAP Handlers?

Any help will be highly appreciated.

回答1:

I have same problem, and could not find another way to remove the tag return of service response, the only way I found was to implement SOAP Handler as follows:

Hope this helps, good luck!

import java.util.Iterator;
import java.util.Set;

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

import org.w3c.dom.NodeList;

import com.millicom.LoggerFFE;
import com.millicom.fulfillment.ws.cin.utils.ConnectorsLoggerUtil;

/**
 * @author jcperez
 * 
 */
public class SOAPLoggingHandler implements SOAPHandler<SOAPMessageContext> {

    protected static LoggerFFE logger=ConnectorsLoggerUtil.getLogger(SOAPLoggingHandler.class);

    @Override
    public boolean handleMessage(SOAPMessageContext context) {

        Boolean outboundProperty = (Boolean) context
                .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

        //Verificamos solo los mensajes de salida
        if(outboundProperty){

            try {
                //Obtenemos el mensaje
                SOAPMessage message = context.getMessage();

                //Obtenemos los elementos del cuerpo
                @SuppressWarnings("unchecked")
                Iterator<Node> it = message.getSOAPBody().getChildElements();

                //Obtenemos el primero nodo, el que corresponde al response de la operacion
                Node node = (Node) it.next();

                //Obtenemos el nodo return
                org.w3c.dom.Node returnNode = node.getChildNodes().item(0);
                //Obtenemos los nodos hijos en una varaible temporal
                NodeList nodeList = returnNode.getChildNodes();
                //Eliminamos el nodo return
                node.removeChild(returnNode);
                //Agregamos los hijos del nodo return al nodo response de la operacion
                while(nodeList.getLength() > 0){
                   node.appendChild(nodeList.item(0));
                }

            } catch (SOAPException e) {
               logger.error("SOAPLoggingHandler#handleMessage(context) Error al obtener el body del mensaje SOAP ", e);
            } catch (Exception e) {
                logger.error("SOAPLoggingHandler#handleMessage(context) Error general ", e);
            }

        }

        return true;
    }

    @Override
    public boolean handleFault(SOAPMessageContext context) {
        //do nothing
        return true;
    }

    @Override
    public void close(MessageContext context) {
      //do nothing
    }

    @Override
    public Set<QName> getHeaders() {
        return null;
    }

}


标签: jax-ws