Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining SAAJ and JAXB

Tags:

java

soap

jaxb

saaj

I'm creating a web service withoug axis. I'm using SAAJ, JAXB and Servlet. I can marshall and unmarshall a class with JAXB correctly. But how can I use together SAAJ and JAXB for SOAP communication. I want put the JAXB converted xml text to SOAP BODY tag with SAAJ. How can I do this? I read SAAJ documents that's on the Oracle site but it isn't understandable. They tell so complex.

like image 463
kodmanyagha Avatar asked Aug 02 '13 11:08

kodmanyagha


1 Answers

You could do the following:

Demo

SOAPBody implements org.w3c.dom.Node so you can have your JAXB implementation marshal to it:

import javax.xml.bind.*;
import javax.xml.soap.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        MessageFactory mf = MessageFactory.newInstance();
        SOAPMessage message = mf.createMessage();
        SOAPBody body = message.getSOAPBody();

        Foo foo = new Foo();
        foo.setBar("Hello World");

        JAXBContext jc = JAXBContext.newInstance(Foo.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.marshal(foo, body);

        message.saveChanges();
        message.writeTo(System.out);
    }

}

Java Model (Foo)

Below is a simple Java model we will use for this example:

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Foo {

    private String bar;

    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }

}

Output

Below is the output from running the demo code (I have formatted it in my answer to make it easier to read).

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header />
    <SOAP-ENV:Body>
        <foo>
            <bar>Hello World</bar>
        </foo>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

UPDATE

Below is an example using JAXB with the JAX-WS APIs (for details on service see: http://blog.bdoughan.com/2013/02/leveraging-moxy-in-your-web-service-via.html).

import javax.xml.bind.JAXBContext;
import javax.xml.namespace.QName;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
import blog.jaxws.provider.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        QName serviceName = new QName("http://service.jaxws.blog/", "FindCustomerService");
        Service service = Service.create(serviceName);
        QName portQName = new QName("http://example.org", "SimplePort");
        service.addPort(portQName, SOAPBinding.SOAP11HTTP_BINDING, "http://localhost:8080/Provider/FindCustomerService?wsdl");

        JAXBContext jc = JAXBContext.newInstance(FindCustomerRequest.class, FindCustomerResponse.class);
        Dispatch<Object> sourceDispatch = service.createDispatch(portQName, jc, Service.Mode.PAYLOAD);
        FindCustomerRequest request = new FindCustomerRequest();
        FindCustomerResponse response = (FindCustomerResponse) sourceDispatch.invoke(request);
        System.out.println(response.getValue().getFirstName());
    }

}
like image 153
bdoughan Avatar answered Oct 18 '22 09:10

bdoughan