How to create and send apache cxf SoapMessage with cxf Messgae?

apache cxf soap client example
apache cxf client example mkyong
spring cxf soap client example
apache cxf wsdl to java example
how apache cxf works
apache cxf soap web service example
apache cxf interceptor
cxf soap header interceptor example

I'm using IntellijIDEA and I have project for testing SOAP WS. I have xml and msg file for sending. How can I cast this file into cxf Message and cxf SoapMessage for sending? This is my method, which return SoapMessage:

public SoapMessage getMessage(File file) throws Exception{
 Message msg;
 msg.setContent(File.class, file);
 SoapMessage message = new SoapMessage(msg);
 return message;

But if I try call this method, I see this:


I try send this msg:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv=""

UPD: What need to msg initiallistion?

Message msg = new Message(){...}?

It use for cast my *.msg file into SoapMessage and send it into SOAP WS

Two ways you can test SOAP based web service

  1. Using SOAP UI client - it will be easy
  2. We can generate the java code using Maven plugin and start consuming the service from main class.
  3. Use wsimport command of Java

    wsimport -keep http://localhost:9090/ws/hello?wsdl

    URL url = new URL("http://localhost:9090/ws/hello?wsdl");
    //1st argument service URI, refer to wsdl document above
    //2nd argument is service name, refer to wsdl document above
    QName qname = new QName("", "HelloWorldImplService");
    Service service = Service.create(url, qname);
    HelloWorld hellObj = service.getPort(HelloWorld.class);

Using obj call appropriate method.

We no need to construct the SOAP message manually.

Apache CXF -- Interceptors, Additionally, in the case of SOAPFaults, a CXF web service will create a separate Writing of the protocol message, such as the SOAP Envelope. After the SEND phase, there are a bunch of "*_ENDING" phases that are symmetrical to the  SoapMessage public SoapMessage(Message message) SoapMessage public SoapMessage(SoapVersion ver) Method Detail. getVersion public SoapVersion getVersion() setVersion public final void setVersion(SoapVersion v) getHeaders public List<Header> getHeaders() hasHeader public boolean hasHeader(QName qn) getHeader public Header getHeader(QName qn

I needed to work on something similar, here is what i've done:

import static;
import static org.junit.Assert.assertThat;

import java.util.List;

import org.apache.cxf.binding.soap.Soap12;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class SOAPMessageTest {

    public void emptyHeaderTest() {
        SoapMessage soapMessage = new SoapMessage(Soap12.getInstance());

        List<Header> headers = soapMessage.getHeaders();
        assertThat(headers.size(), is(0));

Adjust this code to add content to soapMessage object as needed.

Apache CXF -- Developing a Consumer, For CXF clients, the wsdl2java utility can generate the following kinds of code: This contract defines a single port type, Greeter , with a SOAP binding, <wsdl:​input message= "tns:sayHiRequest" name= "sayHiRequest" /> a request context property to set a connection timeout or to initialize data for sending in a header. This does not look very informative. You only see that it is an incoming request (REQ_IN) and the SOAP message in the log message. The logging feature provides a lot more information though. To leverage these the pax logging config can be changed to show the relevant MDC values. Karaf decanter support to write into elastic search

Did you tried to instanciate a soapMessage like this ?

Reader reader = new StringReader(body);
    XMLStreamReader xmlReader = null;
    XMLInputFactory factory = XMLInputFactory.newInstance(); // Or newFactory()
    try {
      xmlReader = factory.createXMLStreamReader(reader);
    } catch (XMLStreamException e) {

    SoapMessage soapMessage = new SoapMessage(Soap11.getInstance());
    soapMessage.setContent(XMLStreamReader.class, xmlReader);

Apache CXF -- How do I integrate my application with CXF, CXF provides a broad variety of touch points which your application can take The SOAP binding, which is the default, maps messages to SOAP and can be used If you use MTOM, CXF will send/receive MIME messages with the message javabeans and instead need to be created or looked up via a custom factory. This post is based on one I wrote a few years ago about building contract first web services with Apache CXF and Spring. The previous post didn't use Spring Boot and most of the Spring and CXF

Apache CXF -- WS-Security, For example, if Bob wants to send a message to Alice, he can encrypt a message These enable creation of a DOM tree for each request/response. need an incoming WSS4J interceptor and only the SOAP client will need an outgoing one. Apache CXF - Logging SOAP Request Response Fault Messages Example 8 minute read Since Apache CXF 3.1, the message logging code was moved into a separate module and gathered a number of new features. In this tutorial, we will demonstrate how to configure CXF to log the SOAP request, response and fault XML using a logging Interceptor and Feature.

Apache CXF -- Developing a Service, Apache CXF, Services Framework - Developing a Service. Start with a WSDL contract and generate Java objects to implement the service. Specifies how the data of the SOAP message is streamed. the wrapper bean for the method parameters that are included in the request message sent in a remote invocation. CXF Example CXF example for routing messages with different transports. The Camel CXF example is a demo of the camel-cxf component to show how to route messages between CXF endpoints, with one endpoint consuming a SOAP over HTTP request while the other providing a SOAP over JMS request for the actual CXF Service endpoint.

Apache CXF -- Provider Services, When using message mode, a Provider implementation works with complete Any response returned from the implementation would also need to be a fully specified SOAP message. Provider implementations receive requests as raw XML data and must send Creates the factories needed for building the response. In message mode, a Dispatch object works with complete messages. A complete message includes any binding specific headers and wrappers. For example, a consumer interacting with a service that requires SOAP messages would need to provide the Dispatch object's invoke() method a fully specified SOAP message.