java - 使用 SOAPHandler 更改请求中的命名空间URI

标签 java soap jax-ws xml-namespaces

我尝试使用 SOAPHandler 更改 SOAP 消息的正文(请求的一部分的命名空间URI),但这似乎不起作用。 更改的原因是,我必须与不同的客户端一起工作,并且有一个旧的客户端无法更改,因此我想更改此客户端消息以使其与我的服务器一起工作。

这是我的代码:

@Override
    public boolean handleMessage(SOAPMessageContext context) {

        boolean isResponse = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        try {
            if (!isResponse) {
                SOAPMessage msg = context.getMessage();
                SOAPBody body = msg.getSOAPBody();
                Iterator<?> iter = body.getChildElements();
                boolean implChanged = false;
                while (iter.hasNext()) {
                    Object next = iter.next();
                    if (next instanceof SOAPElement) {
                        SOAPElement se = (SOAPElement) next;
                        String uri = se.getNamespaceURI();
                        if (uri.equalsIgnoreCase("http://service.plr.mycomp.com/BruttoNettoRechner/1.0")) {
                            if (!se.removeNamespaceDeclaration("impl")) {

                                System.out.println("FEHLER beim entfernen");
                            }
                            if (se.addNamespaceDeclaration("impl", "http://service.plr.mycomp.com") == null) {
                                System.out.println("Fehler beim hinzufügen");
                            }
                            System.out.println(se.getNamespaceURI());
                            System.out.println(se.lookupNamespaceURI("impl"));

                            implChanged = true;

                        }
                    }
                }
                if (msg.saveRequired()) {
                    msg.saveChanges();
                }
                msg.writeTo(System.out);

            }
        } catch (SOAPException e) {
            System.out.println("SOAP FEHLER");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return true;
    }

这是输入:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <impl:calculateBruttoNetto xmlns:impl="http://service.plr.mycomp.com/BruttoNettoRechner/1.0">
         <bruttoNettoIn>
            <ns1:aktBruttoEink xmlns:ns1="http://model.plr.mycomp.com">4000.0</ns1:aktBruttoEink>
            <ns1:aktNettoEink ns2:nil="true" xmlns:ns2="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://model.plr.mycomp.com"/>
            <ns1:alleinverdienerJN xmlns:ns1="http://model.plr.mycomp.com">false</ns1:alleinverdienerJN>
            <ns1:anzGehaelter xmlns:ns1="http://model.plr.mycomp.com">2</ns1:anzGehaelter>
            <ns1:berufsgruppe xmlns:ns1="http://model.plr.mycomp.com">2</ns1:berufsgruppe>
            <ns1:bruttoNetto xmlns:ns1="http://model.plr.mycomp.com">1</ns1:bruttoNetto>
         </bruttoNettoIn>
      </impl:calculateBruttoNetto>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

控制台输出显示 URI 没有更改,但当我询问整个消息时,它似乎有效。但之后的警告表明没有任何改变:

11:08:52,801 INFO  [stdout] (http-/0.0.0.0:8080-1) http://service.plr.mycomp.com/BruttoNettoRechner/1.0

11:08:52,801 INFO  [stdout] (http-/0.0.0.0:8080-1) http://service.plr.mycomp.com/BruttoNettoRechner/1.0

11:08:52,803 INFO  [stdout] (http-/0.0.0.0:8080-1) <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:impl="http://service.plr.mycomp.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Header/><SOAP-ENV:Body>

11:08:52,803 INFO  [stdout] (http-/0.0.0.0:8080-1)       <impl:calculateBruttoNetto xmlns:impl="http://service.plr.mycomp.com">

11:08:52,803 INFO  [stdout] (http-/0.0.0.0:8080-1)          <bruttoNettoIn>

11:08:52,803 INFO  [stdout] (http-/0.0.0.0:8080-1)             <ns1:aktBruttoEink xmlns:ns1="http://model.plr.mycomp.com">4000.0</ns1:aktBruttoEink>

11:08:52,803 INFO  [stdout] (http-/0.0.0.0:8080-1)             <ns1:aktNettoEink xmlns:ns1="http://model.plr.mycomp.com" xmlns:ns2="http://www.w3.org/2001/XMLSchema-instance" ns2:nil="true"/>

11:08:52,803 INFO  [stdout] (http-/0.0.0.0:8080-1)             <ns1:alleinverdienerJN xmlns:ns1="http://model.plr.mycomp.com">false</ns1:alleinverdienerJN>

11:08:52,804 INFO  [stdout] (http-/0.0.0.0:8080-1)             <ns1:anzGehaelter xmlns:ns1="http://model.plr.mycomp.com">2</ns1:anzGehaelter>

11:08:52,804 INFO  [stdout] (http-/0.0.0.0:8080-1)             <ns1:berufsgruppe xmlns:ns1="http://model.plr.mycomp.com">2</ns1:berufsgruppe>

11:08:52,804 INFO  [stdout] (http-/0.0.0.0:8080-1)             <ns1:bruttoNetto xmlns:ns1="http://model.plr.mycomp.com">1</ns1:bruttoNetto>

11:08:52,804 INFO  [stdout] (http-/0.0.0.0:8080-1)          </bruttoNettoIn>

11:08:52,804 INFO  [stdout] (http-/0.0.0.0:8080-1)       </impl:calculateBruttoNetto>

11:08:52,804 INFO  [stdout] (http-/0.0.0.0:8080-1)    </SOAP-ENV:Body>

11:08:52,804 INFO  [stdout] (http-/0.0.0.0:8080-1)    

11:08:52,805 WARNING [org.apache.cxf.phase.PhaseInterceptorChain] (http-/0.0.0.0:8080-1) Interceptor for {http://service.plr.mycomp.com/}BruttoNettoRechnerService#{http://service.plr.mycomp.com}calculateBruttoNetto has thrown exception, unwinding now: org.apache.cxf.interceptor.Fault: Unexpected wrapper element {http://service.plr.mycomp.com/BruttoNettoRechner/1.0}calculateBruttoNetto found.   Expected {http://service.plr.mycomp.com}calculateBruttoNetto.
    at org.apache.cxf.interceptor.DocLiteralInInterceptor.handleMessage(DocLiteralInInterceptor.java:106)

所以我不明白为什么我对命名空间的更改不起作用。 (我不希望该消息起作用,还有其他任务要做)。

谢谢

最佳答案

我没有找到为什么我的代码不起作用,但我找到了一个满足我需求的解决方案:

public boolean handleMessage(SOAPMessageContext context) {

    boolean isResponse = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    try {
        if (!isResponse) {
            SOAPMessage msg = context.getMessage();
            SOAPBody body = msg.getSOAPBody();
            Iterator<?> iter = body.getChildElements();
            while (iter.hasNext()) {
                Object next = iter.next();
                if (next instanceof SOAPElement) {
                    SOAPElement se = (SOAPElement) next;
                    if (se.getNamespaceURI().equalsIgnoreCase("http://service.plr.mycomp.com/BruttoNettoRechner/1.0")) {
                        QName qName = new QName("http://service.plr.mycomp.com", "calculateBruttoNetto", "impl");
                        se.setElementQName(qName);
                        break;
                    }
                }
            }
            if (msg.saveRequired()) {
                msg.saveChanges();
            }
            // msg.writeTo(System.out);

        }
    } catch (SOAPException e) {
        log.error("Fehler beim Bearbeiten des Requests im ServerSOAPHandler", e);
    }

    return true;
}

关于java - 使用 SOAPHandler 更改请求中的命名空间URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20837647/

相关文章:

java - tomcat5上的应用程序无法启动

java - 运行 CXF JAX WS 服务时出现问题

javascript - 没有访问控制允许带有 java rest 后端的原始 ionic 项目

java - Log4j StringMatchFilter 和 denyAll 过滤器属性配置

java - 如何处理 RAW SOAP 响应?

android - 发送 SOAP 请求

web-services - Coldfusion getHttpRequestData 中断了我的 SOAP 调用

Java 后端和 Rails 前端

java - 将图像转换为草图然后卡通化?

java - 如何在 Java EE 容器中设置 LDAP 连接池?