Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 加入SAAJ和JAXB_Java_Soap_Jaxb_Saaj - Fatal编程技术网

Java 加入SAAJ和JAXB

Java 加入SAAJ和JAXB,java,soap,jaxb,saaj,Java,Soap,Jaxb,Saaj,我正在使用Ug axis创建一个web服务。我正在使用SAAJ、JAXB和Servlet。我可以使用JAXB正确地封送和解封一个类。但是,如何将SAAJ和JAXB一起用于SOAP通信呢。我想用SAAJ将JAXB转换的xml文本转换成SOAP BODY标记。我该怎么做?我阅读了Oracle网站上的SAAJ文档,但这是不可理解的。它们告诉我们的是如此复杂。您可以执行以下操作: 演示 SOAPBody实现了org.w3c.dom.Node,因此您可以将JAXB实现封送到它: import javax.

我正在使用Ug axis创建一个web服务。我正在使用SAAJ、JAXB和Servlet。我可以使用JAXB正确地封送和解封一个类。但是,如何将SAAJ和JAXB一起用于SOAP通信呢。我想用SAAJ将JAXB转换的xml文本转换成SOAP BODY标记。我该怎么做?我阅读了Oracle网站上的SAAJ文档,但这是不可理解的。它们告诉我们的是如此复杂。

您可以执行以下操作:

演示

SOAPBody
实现了
org.w3c.dom.Node
,因此您可以将JAXB实现封送到它:

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模型(Foo)

下面是我们将用于此示例的简单Java模型:

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

}
输出

下面是运行演示代码的输出(我在回答中对其进行了格式化,以便于阅读)


你好,世界

更新

下面是一个使用JAXB和JAX-WSAPI的示例(有关服务的详细信息,请参阅:)

import javax.xml.bind.JAXBContext;
导入javax.xml.namespace.QName;
导入javax.xml.ws.Dispatch;
导入javax.xml.ws.Service;
导入javax.xml.ws.soap.SOAPBinding;
导入blog.jaxws.provider.*;
公开课演示{
公共静态void main(字符串[]args)引发异常{
QName serviceName=新的QName(“http://service.jaxws.blog/“,“FindCustomerService”);
Service=Service.create(serviceName);
QName端口QName=新的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 sourceDispatch=service.createDispatch(portQName,jc,service.Mode.PAYLOAD);
FindCustomerRequest=新的FindCustomerRequest();
FindCustomerResponse响应=(FindCustomerResponse)sourceDispatch.invoke(请求);
System.out.println(response.getValue().getFirstName());
}
}

这是一个非常好的答案。所以我找到了另一个api,名为jax-ws。哪条路更好?SAAJ&JAXP还是jax-ws?你在问JAXB的创建者他喜欢什么?你希望他说什么…@StephenD-我是创建JAXB(JSR-222)和EclipseLink JAXB(MOXy)领导团队的一员:)。JAXB是JAX-WS的默认绑定层,因此两者可以很好地协同工作。10分钟前我刚刚问了一个关于JAXB的问题,但我发现您发布的一个答案显示了一个易于理解的示例。谢谢@kodmanyagha—我已经用一个使用JAX-WSAPI的示例更新了我的答案,因此您可以与您的方法进行比较。
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());
    }

}