简体   繁体   中英

CXF fault interceptor - Logging soap fault message as shown in soap clients

I am using apache CXF(spring boot) for developing my soap server. Here I need to log the fault message in my soap Fault interceptor, exactly in the way it is shown in any soap client(ex: Soap UI). How can I log the same output in my fault interceptor ? now it is just showing exception details

Input

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <sayHello xmlns="http://service.sample.com/">
            <GreetingsRequest xmlns="">test</GreetingsRequest>
        </sayHello>
    </Body>
</Envelope>

Output (shown in soap client)

UserNotfound Exception is a custom exception thrown in the code

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <soap:Fault>
            <faultcode>soap:Server</faultcode>
            <faultstring>Fault occurred while processing.</faultstring>
            <detail>
                <ns1:UserNotFoundException xmlns:ns1="http://service.sample.com/">
                    <user xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://service.sample.com/" xsi:type="ns2:user">
                        <userId>U-123</userId>
                        <username>TestUser</username>
                    </user>
                </ns1:UserNotFoundException>
            </detail>
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

My custom interceptor code

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.Phase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CustomSoapFaultInterceptor extends AbstractSoapInterceptor{
    private static final Logger logger = LoggerFactory.getLogger(CustomSoapFaultInterceptor.class);
    public CustomSoapFaultInterceptor() {
        super(Phase.PRE_STREAM);
    }
    @Override
    public void handleMessage(SoapMessage soapMessage) throws Fault {
        Fault fault = (Fault) soapMessage.getContent(Exception.class);
        Throwable faultCause = fault.getCause();
        String faultMessage = fault.toString();
        logger.error("Test Error",fault);
    }
}

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