- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在创建一个没有轴的网络服务。我正在使用 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;
}
}
输出
下面是运行演示代码的输出(我在答案中对其进行了格式化以使其更易于阅读)。
<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>
更新
下面是一个将 JAXB 与 JAX-WS API 结合使用的示例(有关服务的详细信息,请参阅: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());
}
}
关于java - 加入 SAAJ 和 JAXB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18015699/
我的基于 SAAJ 的客户端生成以下 XML 并将其发送到 .NET Web 服务: temp123 temp321 baby000 20 我遇到的问题是,对于 Authentic
我正在尝试将我的项目升级到 java 15。在尝试启动服务器时,我在 bean 创建过程中遇到错误。 util-context.xml: 虚拟适配器.java: private
我有以下错误: Exception: javax.xml.soap.SOAPException: javax.xml.soap.SOAPException: Message send failed:
在我的网络应用程序中,我正在联系一个网络服务(使用 JAX-WS),但我收到以下错误: java.lang.ExceptionInInitializerError at com.sun.xml
我正在创建一个与 Web 服务通信的程序,该程序是使用 JSP 和 Struts 编写的。但是当我想创建一个新的 web 服务实例时,出现以下错误: SEVERE: Servlet.service()
我尝试使用 从 String 创建一个 Soap Envelope SOAPPart().setContent(Source). 我尝试从同一条消息中获取 SOAPBody,但它抛出异常 SEVERE
我想通过 https 调用 Web 服务,并且需要包含客户端证书。我正在用 SAAJ 来做这件事 SOAPConnectionFactory soapConnectionFactory = SOAPC
我有一个基本的 SOAP 服务端点,实际上是 SAP ECC,提供一个服务。我已经使用 SOAPUI 4.5 测试了该服务,并且使用 HTTP Auth 可以正常工作,从事物的外观来看是先发制人的。我
我们需要发送非常巨大的 SOAP 消息(多部分、MTOM),比如大约 1 或 2 GB。有时该过程会出现以下 OOM: java.lang.OutOfMemoryError: Java Heap Sp
我有一个与解析 SOAP 消息相关的问题。 这是消息: ------=_Part_3_5438437.1107262647279 Content-Type: text/xml
我目前正在为我的应用程序使用 Tomcat 服务器,pom.xml 中的以下代码为我提供了适用于 soap 1.2 协议(protocol)的 SAAJ 1.3 版。但是当我们将服务器迁移到 webs
我尝试在 jquery 中向第三方发送 SOAP 请求,但总是出现此错误: soap:ClientProblems creating SAAJ object model 我这样做的方法是将几个标记传递
SAAJ: SOAP with Attachments API for Java MTOM: SOAP消息传输优化机制 我的简单理解:它们处理 SOAP 附件,MTOM 是 SAAJ 的更优化版本。它
我正在使用 SAAJ java 制作一个 Web 服务客户端。我是客户,我询问网络服务的信息。但网络服务受到用户名和密码的保护。 我不知道如何添加这两个。 我在我的代码中尝试了它(请参阅命令),但没有
我正在尝试将一些数据发布到远程计算机,但它返回错误 ns1:Client.NoSOAPAction no SOAPAction header! 我的猜测是我发送 header 元素的方式
SAAJ 基于 DOM,即文档对象模型。这句话是什么意思? AXIOM 基于 StAX,即 XML 的流 API。这句话是什么意思? 哪个更好? 我想澄清关于这两个 API 的概念。谢谢你 最佳答案
我一直致力于创建一个基于 SAAJ 的客户端。一切似乎都工作正常,直到我实现了将附件作为 Web 服务请求的一部分发送的逻辑。 Web 服务操作很简单 - 它需要一个用于文件位置的字符串元素和一个用于
这两个 SOAP 消息有效吗?不同的部分是 namespace 属性 xmlns="http://www.sigvalue.com/acc"。第一个 soap 是一个示例,第二个由 java 代码生成
我正在创建一个没有轴的网络服务。我正在使用 SAAJ、JAXB 和 Servlet。我可以使用 JAXB 正确编码和解码一个类。但是我怎样才能将 SAAJ 和 JAXB 一起用于 SOAP 通信。我想
我正在寻找替代的 SOAP (javax.xml.soap) 实现,而不是 Sun SAAJ .这样做的原因是因为我想在 IBM JDK 5 驱动的 Tomcat AS 上部署 JAX-WS WebS
我是一名优秀的程序员,十分优秀!