Sto creando un servizio Web con l'asse. Sto usando SAAJ, JAXB e Servlet. Posso marshall e unmarshall una classe con JAXB correttamente. Ma come posso usare insieme SAAJ e JAXB per la comunicazione SOAP. Voglio inserire il testo xml convertito JAXB nel tag SOAP BODY con SAAJ. Come posso fare questo? Leggo i documenti SAAJ presenti sul sito Oracle ma non è comprensibile. Dicono così complesso.Unione di SAAJ e JAXB
9
A
risposta
18
Si potrebbe fare la seguente:
Demo
SOAPBody
implementa org.w3c.dom.Node
in modo da poter avere il vostro JAXB implementazione maresciallo ad esso:
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)
Sotto è un modello di Java semplice useremo per questo esempio:
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;
}
}
uscita
Di seguito è l'output di eseguire il codice demo (ho formattato che nella mia risposta per rendere più facile da leggere) .
<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
Di seguito è riportato un esempio utilizzando JAXB con le API JAX-WS (per i dettagli sul servizio vedi: 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());
}
}
è un'ottima risposta. Così ho trovato un'altra API che il nome è jax-ws. quale modo migliore? SAAJ e JAXP o jax-ws? – kodmanyagha
Stai chiedendo al creatore di JAXB cosa preferisce? Cosa ti aspetti che dica ... –
@StephenD - Io faccio parte del gruppo che ha creato JAXB (JSR-222) e il cavo EclipseLink JAXB (MOXy) :). JAXB è il layer di binding predefinito per JAX-WS, quindi i due funzionano bene insieme. –