Search code examples
javawcfcxf

What is the Java Apache CXF equivalent of C# WCF AddressHeader?


I'm trying to access an old SOAP based system written in C# from a Java backend service. This C# application is looking for an AddressHeader to be populated with a specific value on every request. I'm using Apache CXF to create the requests to this service. Unfortunately, for the life of me, I cannot find out how to add this address header to each of the requests. Does anyone know what the equivalent in Java is and how to add it using Apache CXF?


Solution

  • The address header is the same as the SOAP header, hence we only need to add a particular SOAP header to every request so that be capable of making a successful call.
    Here is an example I found on the internet.

    ClientProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    factory.setServiceClass(singz.ws.cxf.sample.SampleServiceInterface.class);
    factory.setAddress("http://xxx.xxx.com/services/SampleService/v1");
            SampleServiceInterface serviceClient = (SampleServiceInterface) factory.create();
            Client proxy = ClientProxy.getClient(serviceClient);
            List<Header> headersList = new ArrayList<Header>();
            Header testSoapHeader1 = new Header(new QName("uri:singz.ws.sample", "soapheader1"), "SOAP Header Message 1", new JAXBDataBinding(String.class));
            Header testSoapHeader2 = new Header(new QName("uri:singz.ws.sample", "soapheader2"), "SOAP Header Message 2", new JAXBDataBinding(String.class));
            headersList.add(testSoapHeader1);
            headersList.add(testSoapHeader2);
            proxy.getRequestContext().put(Header.HEADER_LIST, headersList);
    

    Please refer to the below links, wish it is useful to you.
    https://dzone.com/articles/apache-cxf-how-add-custom-soap-0
    How do you add a Soap Header defined in a wsdl to a web service client in CXF?
    https://dzone.com/articles/apache-cxf-how-add-custom-soap
    Feel free to let me know if there is anything I can help with.