gpt4 book ai didi

java - 处理带有 XML 数据的传入 SOAPMessage 时出错

转载 作者:行者123 更新时间:2023-12-01 22:35:50 30 4
gpt4 key购买 nike

我正在开发一个网络服务,它将两个字符串值作为输入参数。第一个标识用于导入 xml 数据的服务,第二个是 XML 数据字符串。这是 SOAP 请求的示例

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservices.dataexchange.xxx.com/">
<soapenv:Header/>
<soapenv:Body>
<web:importDataExchange>
<ServiceID>XmlImportServiceName</ServiceID>
<ExchangeData><?xml version="1.0" encoding="UTF-8"?><srv:exchange xmlns:cnt="Container" xmlns:core="Core" xmlns:srv="Service" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Service http://xxx/xxx.xsd"></srv:exchange></ExchangeData>
</web:importDataExchange>
</soapenv:Body>
</soapenv:Envelope>

不幸的是,每当我尝试解析 SOAPMessage 时,都会收到以下错误:

javax.xml.bind.UnmarshalException
- with linked exception:
[com.ctc.wstx.exc.WstxParsingException: Illegal processing instruction target ("xml"); xml (case insensitive) is reserved by the specs.
at [row,col {unknown-source}]: [6,28]]

这显然是因为我实现的 JAX-WS 正在尝试解码 <ExchangeData> 的内容元素。当然,这都可以通过包装 <ExchangeData> 的内容来解决。在<![CDATA[ ]]>元素,但是,我无法控制数据如何发送到服务器。因此,我需要一种方法来拦截 SOAPMessage 并将 ExchangeData 提取为字符串而不对其进行解码。或者,我需要一种方法让 Web 服务将 ExchangeData 节点的内容视为一个字符串,而不是 SOAPMessage 主体/负载中的另一个 XML 节点。

我已尝试实现 SOAPHandler 和 LogicalHandler,但在这两种情况下,每当获取消息/有效负载时,Web 服务器都会尝试解码 SOAP 消息,这会再次产生相同的错误。

这是我目前开发的 ImportServiceEndpoint。

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

@WebService(serviceName = "ImportServiceEndpoint", portName = "dataexchange")
// @HandlerChain(file = "handlers.xml")
public class ImportServiceEndpoint extends SpringBeanAutowiringSupport {

private static final Logger LOGGER = LoggerFactory.getLogger(ImportServiceEndpoint.class);

/**
* The soap implementation of the data exchange service.
*
* @param serviceID
* the id of the service
* @param exchangeData
* the xml data for the request.
* @return a response about the success/failure of the dataexchange.
*/
@WebMethod
@WebResult(name = "importDataExchangeResponse")
public String importDataExchange(@ModelAttribute("ServiceID") @WebParam(name = "ServiceID") final String serviceID,
@ModelAttribute("ExchangeData") @WebParam(name = "ExchangeData") final String exchangeData) {
LOGGER.debug("Recevied Soap request for service {} with xml data '{}'", serviceID, exchangeData);
return "Message Received";
}
}

如有任何帮助,我将不胜感激。我感到非常沮丧,因为我已经为此工作了几天,但几乎没有什么成果。

最佳答案

我的问题是 xml 文件由空行和 <?xml version="1.0" encoding="UTF-8"?> 开始不是文件的开头。

去掉空行,问题解决。

关于java - 处理带有 XML 数据的传入 SOAPMessage 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23758823/

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