gpt4 book ai didi

java - 删除 JAX-WS 消息中的 XML 声明

转载 作者:行者123 更新时间:2023-12-02 04:07:15 25 4
gpt4 key购买 nike

我正在尝试使用 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-WSJAXB生成的。

提前致谢!

最佳答案

找到了我自己的解决方案!

首先,正如其他文章中提到的,我实现了一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com