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

标签 java xml web-services soap

我正在开发一个网络服务,它将两个字符串值作为输入参数。第一个标识用于导入 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/

相关文章:

java - 将项目从一个 JComboBox 传输到另一个 JComboBox

java - 如何在 Spring 中实现 GraphQL 的异常处理程序

c# - 将 XML 值反序列化为枚举时处理额外空格

java - 有没有办法在 JdbcTemplate 上智能设置获取大小?

java - Java 新手 : what are the bits between i and j in 10000000000

xml - 如何使用 XML::LibXML::Reader 获取属性名称列表?

sql - 在 SQL 中读取 XML BOOLEAN 值

java - 如何使用 Spring RESTful Web 服务观看视频

reactjs - 在某些不支持 BroadcastChannel 的浏览器中,从 Service Worker 发送消息以响应组件

c# - 存储库模式