Advertise with us

Our eBooks

Apache CXF Interceptors

There are two ways to apply CXF Interceptors in your applications:

1) The recommended approach is to configure them in the jaxws-endpoint-config.xml descriptor of your application or, if you want to set them for all endpoints, in your webservices sibsystem

2) The legacy approach is to use CXF annotations in your code. This can be applied for older WildFly versions (versions 8-10)

Let's start from the first approach. You can include the jaxws-endpoint-config.xml descriptor in the WEB_INF folder of your application. Within this file, you will specify a list of interceptor class names for the "" and "cxf.interceptors.out". Example:

<?xml version="1.0" encoding="UTF-8"?>
<jaxws-config xmlns="urn:jboss:jbossws-jaxws-config:4.0" xmlns:xsi="" xmlns:javaee=""
  xsi:schemaLocation="urn:jboss:jbossws-jaxws-config:4.0 schema/jbossws-jaxws-config_4_0.xsd">

As an alternative, you can set the same properties in the webservices subsystem of your WildFly configuration:

  <subsystem xmlns="urn:jboss:domain:webservices:2.0">
            <endpoint-config name="Standard-Endpoint-Config">
                <property name="" value="com.sample.DemoRequestInterceptor"/>

Using CXF Annotations in your Web services (deprecated)

Interceptors can be added either via configuration, via annotations or even programmatically. The simplest of all is maybe using annoations. The following example uses two custom interceptors Test1Interceptor which is bound to the Server Incoming Chain and Test2Interceptor which is bound to the Outgoing Chain:

@org.apache.cxf.interceptor.InInterceptors (interceptors = {"com.example.Test1Interceptor" })
@org.apache.cxf.interceptor.OutInterceptors (interceptors = {"com.example.Test2Interceptor" })
public class SayHiImpl  implements SayHi {
   public long sayHi(long arg) {
     return arg;

and here's how you can configure these interceptors using cxf.xml configuration file:

<beans xmlns=""

 <bean id="MyInterceptor1" class="com.example.Test1Interceptor"/>
 <bean id="MyInterceptor2" class="com.example.Test2Interceptor"/>

     <ref bean="MyInterceptor1"/>
     <ref bean="MyInterceptor2"/>

Adding Fault Interceptors to your Web services

In the case of SOAPFaults, a CXF web service will create a separate outbound error handling chain and the client will create an inbound error handling chain. By default, the last interceptor in the outbound fault chain is Soap11FaultOutInterceptor which  automatically appends the details of the SOAPFault into the response. This might now be always desirable as we might want to display a custom error message.

In order to do that you need simply to add a custom OutFaultInterceptors which modifies the details of the SOAPFault. There are many places that we could do this, for example you could do that before the final interceptor (Soap11FaultOutInterceptor) is called.

public class CustomFaultInterceptor extends AbstractSoapInterceptor {

   public CustomFaultInterceptor() {

   public void handleMessage(SoapMessage message) throws Fault {
     Fault fault = (Fault) message.getContent(Exception.class);


And here's how you can bind this Interceptor to your Web service using annotations:

@org.apache.cxf.interceptor.OutFaultInterceptors (interceptors = {"com.sample.CustomFaultInterceptor" })
public class SayHiImpl  implements SayHi {
    public long sayHi(long arg) {
      return arg;
© 2021 All Rights Reserved.

Please publish modules in offcanvas position.