- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Java 代码调用 Web 服务。因此,我使用 JAX-WS 和 JAXB 从 wsdl 文件生成对象。
当我调用 Web 服务时,它会响应以下错误:
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: The [javax.xml.transform.TransformerException] occurred during XSLT transformation: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException: The XML declaration must end with "?>".
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: The [javax.xml.transform.TransformerException] occurred during XSLT transformation: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException: The XML declaration must end with "?>".
at com.sun.xml.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:189)
at com.sun.xml.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:122)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:119)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:89)
at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:118)
因此,我使用wireshark 分析了正在发送的xml 消息。并尝试用soapUI重新发送它。
并发现我的xml包含xml声明
<?xml version='1.0' encoding='UTF-8'?>
当我从 SoapUI 中删除它并重新发送它时。消息正常。
我的java代码是这样的:
public static Data receiveSIBS(webserviceclient.Data input) {
webserviceclient.Starter service = new webserviceclient.Starter();
webserviceclient.PortType port = service.getSOAPEventSource();
BindingProvider bp = (BindingProvider) port;
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpoint);
return port.receiveSIBS(input);
}
如果没有这个 xml 声明,如何用 Java 生成消息?因为xml消息都是用JAX-WS和JAXB生成的。
提前致谢!
最佳答案
找到了我自己的解决方案!
首先,正如其他文章中提到的,我实现了一个 SOAPHandler 来编辑这两个属性:
soapMsg.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-16");
soapMsg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "false");
但是,虽然这两个属性改变了handleMessage()方法中的消息实例,但它不会像这样发送,而是发送带有默认xml声明的消息。
解决方案不是设置此属性,而是设置这两个 NamespaceDeclaration:
SOAPEnvelope env = sp.getEnvelope();
env.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
env.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
我不明白为什么我们会收到“XML 声明必须以“?>”结尾”错误。因为我的解决方案没有删除 xml 声明。可能与xml结构有关(但我没有足够的知识来确认它)。
我需要引用http://blog.jdevelop.eu/?p=67这篇文章让我了解了这个解决方案,一些调试代码来自这篇文章。
接下来,我放置了完整的 CustomHandler 类,以便它可以容纳任何人。
import java.io.ByteArrayOutputStream;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
/**
*
* @author Daniel Chang Yan
*/
public class CustomHandler implements SOAPHandler<SOAPMessageContext> {
public boolean handleMessage(SOAPMessageContext context) {
Boolean isOutbound
= (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (isOutbound != null && isOutbound) {
SOAPMessage soapMsg = context.getMessage();
try {
//Properties always rewritten by jaxws, no matter what is set here
//soapMsg.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-16");
//soapMsg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "false");
// get SOAP-Part
SOAPPart sp = soapMsg.getSOAPPart();
//edit Envelope
SOAPEnvelope env = sp.getEnvelope();
env.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
env.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
} catch (SOAPException e) {
throw new RuntimeException(e);
}
// print SOAP-Message
System.out.println("Direction=outbound (handleMessage)...");
dumpSOAPMessage(soapMsg);
} else {
// INBOUND
System.out.println("Direction=inbound (handleMessage)...");
SOAPMessage msg = ((SOAPMessageContext) context).getMessage();
dumpSOAPMessage(msg);
}
return true;
}
public Set<QName> getHeaders() {
return null;
}
public boolean handleFault(SOAPMessageContext context) {
System.out.println("ServerSOAPHandler.handleFault");
boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outbound) {
System.out.println("Direction=outbound (handleFault)...");
} else {
System.out.println("Direction=inbound (handleFault)...");
}
if (!outbound) {
try {
SOAPMessage msg = ((SOAPMessageContext) context).getMessage();
dumpSOAPMessage(msg);
if (context.getMessage().getSOAPBody().getFault() != null) {
String detailName = null;
try {
detailName = context.getMessage().getSOAPBody().getFault().getDetail().getFirstChild().getLocalName();
System.out.println("detailName=" + detailName);
} catch (Exception e) {
}
}
} catch (SOAPException e) {
e.printStackTrace();
}
}
return true;
}
public void close(MessageContext mc) {
}
/**
* Dump SOAP Message to console
*
* @param msg
*/
private void dumpSOAPMessage(SOAPMessage msg) {
if (msg == null) {
System.out.println("SOAP Message is null");
return;
}
//System.out.println("");
System.out.println("--------------------");
System.out.println("DUMP OF SOAP MESSAGE");
System.out.println("--------------------");
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
msg.writeTo(baos);
System.out.println(baos.toString(getMessageEncoding(msg)));
// show included values
String values = msg.getSOAPBody().getTextContent();
System.out.println("Included values:" + values);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Returns the message encoding (e.g. utf-8)
*
* @param msg
* @return
* @throws javax.xml.soap.SOAPException
*/
private String getMessageEncoding(SOAPMessage msg) throws SOAPException {
String encoding = "utf-8";
if (msg.getProperty(SOAPMessage.CHARACTER_SET_ENCODING) != null) {
encoding = msg.getProperty(SOAPMessage.CHARACTER_SET_ENCODING).toString();
}
return encoding;
}
}
关于java - 删除 JAX-WS 消息中的 XML 声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34154901/
我有一个使用 spring-ws 的 web 服务(服务器端)的实现。我想更改我的代码以使用 spring-integration-ws(ws:入站网关和 channel )。我已经尝试过示例,但仍然
我正在尝试使用 JAX-WS (Metro) 开发一个独立的 Java Web 服务客户端,它使用 WS-Security 和用户名 token 身份验证(密码摘要、随机数和时间戳)和时间戳验证以及
我开始使用 JAX WS 研究 Java Web 服务。我正在阅读的书的第一章展示了如何仅使用 java SE 构建和部署简单的 jax ws web 服务。特别是,Web 服务是通过 Endpoin
我尝试在 Netbean 6.8 中使用 ws-import 生成 Java 类。我想重新生成 jax-ws,因为在 ebay.api.paypalapi 包中发现了一个错误(我认为该错误是由于 Pa
我正在为我的项目编写服务器端,它需要 websockets 提供的功能。我一直在寻找并在 npm 中找到了 3 个库。 ws、websocket 和express-ws。您能否解释一下它们之间的区别并
我有一个用于测试 Web 服务的项目,它使用 spring-ws 库。spring-ws 在不久的将来会支持 WS-Reliable Messaging 吗? 最佳答案 作为 Spring-WS 的领
我在 Tomcat 上部署了一个有状态的 Web 服务。它由工厂服务和主要 API 服务组成,并且运行良好。工厂服务向主 API 实例返回一个 W3CEndpointReference,客户端使用该
我构建了一个最小的 Web 服务并使用 javax.xml.ws.Endpoint 发布它。如果我尝试在http://localhost:1234/AddService?wsdl 工作正常。 尝试在
我正在实现一个必须支持 WS-Trust (1.3) 的网络服务。 OASIS 将 WS-Trust 的命名空间定义为 http://docs.oasis-open.org/ws-sx/ws-trus
有没有办法操纵jaxws中使用的编码器。 我喜欢发送一个在网络服务请求中提交的 cdata,为此我想在这里尝试类似描述的东西:http://odedpeer.blogspot.de/2010/07/j
我正在尝试使用 Spring-WS 构建一个简单的 Web 服务客户端,但遇到了麻烦。我试图调用的 SOAP 服务使用 HTTP 基本身份验证以确保安全。 使用 Spring-WS 教程示例,我已经配
我对网络服务很陌生。我找不到 JAX-WS 和 CXF 之间的区别。 据我了解,JAX-WS是java提供的规范,CXF是实现。 如果我错了,请纠正我。 最佳答案 是的,你是对的。 JAX-WS 是基
我有很多用 @WebService(targetNamespace = "mynamespace") 注释的端点.每@WebResult和 @WebParam与 targetNamespace = "
根据网上的文献,我可以看到有两个与Web Services Eventing相关的规范: WS-BaseNotification - 由 OASIS 于 2004 年提交 WS-Eventing -
对于基于 SOAP 的 Web 服务,为什么应该选择 Spring WS 而不是 JAX-WS。我已经阅读了一些文章,甚至 Spring WS 文档功能,但我仍然不清楚。如果我需要说服某人使用 Spr
我使用 wsimport 创建了一个肥皂客户端,我需要将消息中字符串字段内的 xml 数据发送到网络服务器。我知道我实际上并不需要在 Web 服务调用中使用 cdata,但 Web 服务需要此字段位于
我想问一下如何在 JAX-WS 中指定 SOAP Web 服务,这样如果我希望通过值 A 或值B。任何其他值都应该返回错误。 我想到了Java中的枚举,但仍然不知道如何实现它。 有什么建议吗?预先感谢
我用 pyws 实现了一个简单的 Web 服务服务器和 suds . from pyws.server import SoapServer from pyws.functions.register i
我正在 javascript 中使用 websockets 连接到 API 并读取一些数据。 这是代码: function processHUOBIAPI(client,exchange_name,e
我正在使用 Node.js + Express + Express-ws 创建应用程序,但在连接后尝试发送消息时出现以下错误: const fs = require('fs'); con
我是一名优秀的程序员,十分优秀!