Java 使用SOAPHandler更改请求中的namespaceURI

Java 使用SOAPHandler更改请求中的namespaceURI,java,soap,jax-ws,xml-namespaces,Java,Soap,Jax Ws,Xml Namespaces,我尝试用SOAPHandler更改soap消息的主体(请求部分的namespaceURI),但这似乎不起作用。 更改的原因是,我必须与不同的客户端一起工作,并且有一个旧的客户端无法更改,因此我希望更改此客户端消息以使其与我的服务器一起工作 这是我的密码: @Override public boolean handleMessage(SOAPMessageContext context) { boolean isResponse = (Boolean) context.g

我尝试用SOAPHandler更改soap消息的主体(请求部分的namespaceURI),但这似乎不起作用。 更改的原因是,我必须与不同的客户端一起工作,并且有一个旧的客户端无法更改,因此我希望更改此客户端消息以使其与我的服务器一起工作

这是我的密码:

@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;
    }
@覆盖
公共布尔handleMessage(SOAPMessageContext上下文){
boolean isResponse=(boolean)context.get(MessageContext.MESSAGE\u OUTBOUND\u属性);
试一试{
如果(!isResponse){
SOAPMessage msg=context.getMessage();
SOAPBody=msg.getSOAPBody();
迭代器iter=body.getChildElements();
布尔值=假;
while(iter.hasNext()){
Object next=iter.next();
if(SOAPElement的下一个实例){
SOAPElement se=(SOAPElement)next;
字符串uri=se.getNamespaceURI();
if(uri.equalsIgnoreCase(“http://service.plr.mycomp.com/BruttoNettoRechner/1.0")) {
如果(!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();
}
消息写入(系统输出);
}
}捕获(SOAPE例外){
System.out.println(“SOAP-FEHLER”);
}捕获(IOE异常){
e、 printStackTrace();
}
返回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>

4000
假的
2.
2.
1.
控制台输出表明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)
11:08:52801信息[stdout](http-/0.0.0:8080-1)http://service.plr.mycomp.com/BruttoNettoRechner/1.0
11:08:52801信息[标准输出](http-/0.0.0:8080-1)http://service.plr.mycomp.com/BruttoNettoRechner/1.0
11:08:52803信息[标准输出](http-/0.0.0:8080-1)
11:08:52803信息[标准输出](http-/0.0.0:8080-1)
11:08:52803信息[标准输出](http-/0.0.0:8080-1)
11:08:52803信息[标准输出](http-/0.0.0:8080-1)4000.0
11:08:52803信息[标准输出](http-/0.0.0:8080-1)
11:08:52803信息[stdout](http-/0.0.0:8080-1)错误
11:08:52804信息[标准输出](http-/0.0.0:8080-1)2
11:08:52804信息[标准输出](http-/0.0.0:8080-1)2
11:08:52804信息[标准输出](http-/0.0.0:8080-1)1
11:08:52804信息[标准输出](http-/0.0.0:8080-1)
11:08:52804信息[标准输出](http-/0.0.0:8080-1)
11:08:52804信息[标准输出](http-/0.0.0:8080-1)
11:08:52804信息[标准输出](http-/0.0.0:8080-1)
11:08:52805警告[org.apache.cxf.phase.PhaseInterceptorChain](http-/0.0.0:8080-1)用于{http://service.plr.mycomp.com/}BruttoNettoRechnerService#{http://service.plr.mycomp.com}CalculateButto引发了异常,正在展开:org.apache.cxf.interceptor.Fault:意外的包装器元素{http://service.plr.mycomp.com/BruttoNettoRechner/1.0}找不到CalculateButtoneTo。应为{http://service.plr.mycomp.com}计算按钮。
位于org.apache.cxf.interceptor.docliteralinterceptor.handleMessage(docliteralinterceptor.java:106)
所以我不明白为什么我对名称空间的更改不起作用(我不希望消息起作用,还有其他任务要做)


thx

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

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;
}
公共布尔handleMessage(SOAPMessageContext上下文){ boolean isResponse=(boolean)context.get(MessageContext.MESSAGE\u OUTBOUND\u属性); 试一试{ 如果(!isResponse){ SOAPMessage msg=context.getMessage(); SOAPBody=msg.getSOAPBody(); 迭代器iter=body.getChildElements(); while(iter.hasNext()){ Object next=iter.next(); if(SOAPElement的下一个实例){ SOAPElement se=(SOAPElement)next; if(se.getNamespaceURI().equalsIgnoreCase(“http://service.plr.mycomp.com/BruttoNettoRechner/1.0")) { QName QName=新的QName(“http://service.plr.mycomp.com“,“CalculateButto”,“impl”); se.setElementQName(qName); 打破 } } } if(msg.saveRequired()){ msg.saveChanges(); } //消息写入(系统输出); } }捕获(SOAPE例外){ log.error(“Fehler beim Bearbeiten des Requests im ServerSOAPHandler”,e); } 返回true; }