Joining SAAJ and JAXB
Asked Answered
A

1

10

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.

Aachen answered 2/8, 2013 at 11:23 Comment(0)
B
20

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

}
Beeck answered 2/8, 2013 at 13:26 Comment(8)
that's very great answer. So I found an another api that name is jax-ws. which way better? SAAJ & JAXP or jax-ws?Aachen
You're asking the creator of JAXB what he prefers? What do you expect him to say...Facelift
@StephenD - I am part of the group that created JAXB (JSR-222), and the EclipseLink JAXB (MOXy) lead :). JAXB is the default binding layer for JAX-WS so the two do work nicely together.Beeck
I had just asked a question about JAXB 10 minutes ago, but I found an answer you had posted that showed an example that was easy to follow. Thanks!Facelift
@Aachen - I have updated my answer with an example using the JAX-WS APIs so you have something to compare your approach with.Beeck
SOAPBody doesn't implement org.w3c.dom.Node, but javax.xml.soap.Node. The example has compile errorsStefaniestefano
@Stefaniestefano - SOAPBody does extend org.w3c.dom.Node: docs.oracle.com/javaee/5/api/javax/xml/soap/SOAPBody.htmlBeeck
You are right. Someone checked in very old version of SOAPBody into our project and I thought it is from JDK. Insane...Stefaniestefano

© 2022 - 2024 — McMap. All rights reserved.