阅读背景:

如何使用cxf Messgae创建和发送apache cxf SoapMessage?

来源:互联网 

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:

我正在使用IntellijIDEA,我有测试SOAP WS的项目。我有发送的xml和msg文件。如何将此文件转换为cxf消息和cxf SoapMessage进行发送?这是我的方法,它返回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="https://schemas.xmlsoap.org/soap/envelope/"
schemaLocation="https://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
    <Test>
      <msg>Test</msg>
    </Test>
</soapenv:Body>
</soapenv:Envelope>

UPD: What need to msg initiallistion?

UPD:msg initiallistion需要什么?

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

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

它用于将my * .msg文件转换为SoapMessage并将其发送到SOAP WS中

3 个解决方案

#1


Two ways you can test SOAP based web service

有两种方法可以测试基于SOAP的Web服务

  1. Using SOAP UI client - it will be easy
  2. 使用SOAP UI客户端 - 这将很容易

  3. We can generate the java code using Maven plugin and start consuming the service from main class.
  4. 我们可以使用Maven插件生成java代码并开始使用主类中的服务。

  5. Use wsimport command of Java

    使用Java的wsimport命令

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

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

    URL url = new URL("https://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("https://ws.service.com/", "HelloWorldImplService");
    Service service = Service.create(url, qname);
    
    HelloWorld hellObj = service.getPort(HelloWorld.class);
    

Using obj call appropriate method.

使用obj调用适当的方法。

We no need to construct the SOAP message manually.

我们不需要手动构造SOAP消息。

#2


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.

调整此代码以根据需要向soapMessage对象添加内容。

#3


Did you tried to instanciate a soapMessage like this ?

您是否试图像这样实现soapMessage?

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);

分享到: