在soap消息中获得了额外的属性,迁移了Java 7-> Java 11问题

时间:2020-05-07 14:53:47

标签: java gradle soap libs

我得到了以下代码:

public class MyHandler implements SOAPHandler<SOAPMessageContext> {
...
    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        Boolean outBoundProperty = (Boolean) context
            .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        try {
            if (outBoundProperty) {
                return processOutputMessage(context);

            } else {
                return processInputMessage(context);
            }
        } catch (SOAPException | IOException e) {
            messagesLog.error(e, e);
            reportLog.error(e,e);
            return false;
        }
    }
...
    private boolean processOutputMessage(SOAPMessageContext context) throws SOAPException, IOException {
        Logger localLogger;
        OutputFormat outputFormat = new OutputFormat();
        outputFormat.setExpandEmptyElements(true);
        SOAPMessage msg = context.getMessage();
        SOAPHeader header = msg.getSOAPHeader();
        Element rootElement = (Element) header.getChildNodes().item(0);
        Element signatureElement = (Element) rootElement.getChildNodes().item(0);
        Document dom4jDocument = signatureElement.getOwnerDocument();
        org.dom4j.io.DOMReader reader = new DOMReader();
        org.dom4j.Document document = reader.read(dom4jDocument);
        if (isRegistry(document)) {
            localLogger = reportLog;
        } else {
            localLogger = messagesLog;
        }
        org.dom4j.Node node = document.selectSingleNode("/*[local-name()='Envelope']/*[local-name()='Body']/*");
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        XMLWriter writer = new XMLWriter(stream, outputFormat);
        writer.write(node);
        String bodyString = new String(stream.toByteArray());
        String sign = signer.sign(bodyString.getBytes()/*, signAlgorithm*/);
        signatureElement.setTextContent(sign);
        localLogger.info("Request: " + nodeToString(msg.getSOAPPart()));
        return true;
    }

使用Java 7,我得到了正确的生成请求。 更新到Java 11后,我必须使用jaxsws-rt和其他一些库,因为删除了javax.xml.ws。

前后有区别:

After:  <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
Before: <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">

After:  <S:Header xmlns="*">
Before: <S:Header>

After:  <S:Body xmlns="*">
Before: <S:Body>

我试图使用旧图书馆。我从旧的jdk中获取了rt.jar,并将其与gradle的实现文件一起使用,但是在运行时,我可以看到正在使用jaxws-rt.jar。而且我认为以这种方式解决问题是一个糟糕的解决方案。无论如何,我想知道是否有一种方法可以在不删除jaxws-rt.jar的情况下强制使用rt.jar,因为会发生ClassNotFoundException

我确信可以用几行代码来解决我的问题,所以请帮助我

我还添加了这些行

        msg.getSOAPHeader().removeAttribute("xmlns");
        msg.getSOAPBody().removeAttribute("xmlns");

它有效,我的请求已成功处理。但是仍然有额外的属性

0 个答案:

没有答案