简体   繁体   中英

How to get SOAP fault message have no mapped in wsdl

I generated my client soap from wsimport JAX-WS, I have already consumed others webservice that it had fault message mapped, but the service current doesn't have.

When I call the service and it returns fault message I can't get the message in the Java, but if call from soapUI I can see the error.

The fault message is the same of the success, generated from JAX-WS.

My code:

//before I setter my request

try{
IPGApiOrderService iPGApiOrderService = new IPGApiOrderService();
IPGApiOrder client = iPGApiOrderService.getIPGApiOrderSoap11();
IPGApiOrderResponse response = client.ipgApiOrder(request)
}catch (SOAPFaultException soapEx) {
System.out.println("Fault ............. " + soapEx.getFault());
System.out.println("Detail ............ " + soapEx.getFault().getDetail());
System.out.println("FaultCode.......... " + soapEx.getFault().getFaultCode());
System.out.println("FaultActor......... " + soapEx.getFault().getFaultActor());
System.out.println("Message............ " + soapEx.getMessage());
soapEx.printStackTrace();
}

follow the out

Fault ............. [SOAP-ENV:Fault: null]
Detail ............ [detail: null]
FaultCode.......... SOAP-ENV:Client
FaultActor......... null
Message............ Client received SOAP Fault from server: ProcessingException Please see the server log to find more detail regarding exact cause of the failure.
com.sun.xml.internal.ws.fault.ServerSOAPFaultException: Client received SOAP Fault from server: ProcessingException Please see the server log to find more detail regarding exact cause of the failure.
    at com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:178)
    at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:124)
    at com.sun.xml.internal.ws.client.sei.StubHandler.readResponse(StubHandler.java:238)
    at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:189)
    at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:276)
    at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:104)
    at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:77)
    at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:147)
    at com.sun.proxy.$Proxy36.ipgApiOrder(Unknown Source)
    at com.firstdata.test.demo.MainTest.main(MainTest.java:53)

I resolved my problem with following steps.

  1. Create SOAPHandler;
  2. It'll be necessary implement 4 methods;
  3. On method handleFault get SOAPMessageContext -> SOAPMessage -> SOAPBody -> Fault -> Detail -> add detail with xml error or some information do you want.

3.1 Fault you can put fault code, if API you was consuming always return one code error to API fault. 4. On exception you find that detail you set and work with it.

Code:

public class SOAPHandlerImpl implements SOAPHandler<SOAPMessageContext> {

    public static final QName JSON_ERROR = new QName("json-error");

    public boolean handleMessage(SOAPMessageContext smc) {
        SOAPMessage message = smc.getMessage();
        Boolean isOut = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        return isOut;
    }

    @Override
public boolean handleFault(SOAPMessageContext context) {
    SOAPMessage message = context.getMessage();
    try {
        StringOutputStream str = new StringOutputStream();
        message.writeTo(str);
        ErrorDTO dto = XmlUtil.buildErroDTO(str.toString());
        Detail detail = message.getSOAPBody().getFault().getDetail();
        Gson gson = new Gson();
        String obj = gson.toJson(dto);
        detail.addDetailEntry(JSON_ERROR).addTextNode(obj);
        message.getSOAPBody().getFault().setFaultCode(String.valueOf(dto.getTransactionId()));
    } catch (Exception e) {
        System.out.println("Exception in handler: " + e);
    }
    return true;
}

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

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

Catch exception

} catch (SOAPFaultException sopex) {
            ErrorDTO error = null;
            Iterator childElements = sopex.getFault().getDetail().getChildElements();
            while (childElements.hasNext()) {
                DetailEntry next = (DetailEntry) childElements.next();
                if (SOAPHandlerImpl.JSON_ERROR.getLocalPart().equals(next.getNodeName())) {
                    Gson gson = new Gson();
                    error = gson.fromJson(next.getValue(), ErrorDTO.class);
                }
            }
            String message = null;
            if(error.getProcessorResponseCode() != null) {
                message = ErrorApiUtil.getInstance().getMessage(error.getProcessorResponseCode());
            }else {
                message = error.getMessage();
            }
            throw new BusinessException(message);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM