简体   繁体   中英

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

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:

java.lang.NullPointerException

I try send this msg:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
schemaLocation="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
    <Test>
      <msg>Test</msg>
    </Test>
</soapenv:Body>
</soapenv:Envelope>

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("http://ws.service.com/", "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.

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

import static org.hamcrest.CoreMatchers.is;
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;

@RunWith(BlockJUnit4ClassRunner.class)
public class SOAPMessageTest {

    @Test
    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.

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) {
      e.printStackTrace();
    }

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

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