Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

703
Views
Spring-Ws Remove namespace prefix from responses (ns2, ns3...)

I know this has been asked 100k times. But I do not manage to do it. Response always comes as follows:

<SOAP-ENV:Header/>

<SOAP-ENV:Body>

    <ns2:getReportResponse xmlns:ns2="http://www.example.net/myresource">

        <ns2:Report>JVBE...</ns2:Report>

        <ns2:Messages/>

    </ns2:getReportResponse>

</SOAP-ENV:Body>

And I want to get:

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

<SOAP-ENV:Header/>

<SOAP-ENV:Body>

    <getReportResponse xmlns="http://www.example.net/example">

        <Report>JVBE...</Report>

        <Messages/>

    </getReportResponse>

</SOAP-ENV:Body>

I already tried setting elementFormDefault="unqualified" without any kind of success (I got empty responses when this was done...). Any ideas? @NameSpace(prefix="" namespace=NAMESPACE_URI) did not help either...

Thanks in advance.

Ps. I would love to use a pure Spring-WS solution, instead of using any kind of marshallers and so on from jaxb

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I managed to fix it myself. Even though I did not get an answer from the community I would like to write the answer here so no one has to struggle through what I have today.

I am not sure if there is a way for achieving this directly using a magical Spring @notation. Nonetheless, I managed to do this myself as follows:

I created an EndpointInterceptor where I implemented a post processor to remove the unwanted prefixes.

public class GlobalEndpointInterceptor implements EndpointInterceptor {
    @Override
    public boolean handleRequest(MessageContext messageContext, Object o) throws Exception {
        return true;
    }

    @Override
    public boolean handleResponse(MessageContext messageContext, Object o) throws Exception {
        return true;
    }

    @Override
    public boolean handleFault(MessageContext messageContext, Object o) throws Exception {
        return false;
    }

    @Override
    public void afterCompletion(MessageContext messageContext, Object o, Exception e) throws Exception {
        try {
            SOAPMessage soapMessage = ((SaajSoapMessage) messageContext.getResponse()).getSaajMessage();
            SOAPHeader header = soapMessage.getSOAPHeader();
            SOAPBody body = soapMessage.getSOAPBody();
            header.setPrefix("");
            body.setPrefix("");
            Iterator<SOAPBodyElement> it = body.getChildElements();
            while (it.hasNext()) {
                SOAPBodyElement node = it.next();
                node.setPrefix("");
                Iterator<SOAPBodyElement> it2 = node.getChildElements();
                while (it2.hasNext()) {
                    SOAPBodyElement node2 = it2.next();
                    node2.setPrefix("");
                    Iterator<SOAPBodyElement> it3 = node2.getChildElements();
                    while (it3.hasNext()) {
                        SOAPBodyElement node3 = it3.next();
                        node3.setPrefix("");
                    }
                }
            }
        } catch (SOAPException exx) {
            exx.printStackTrace();
        }
    }
}

And then simply added the implemented interceptor to the WS configuration as follows:

    @Configuration
    @EnableWs
    public class ExampleConfiguration extends WsConfigurerAdapter {

        @Override
        public void addInterceptors(List<EndpointInterceptor> interceptors) {
            interceptors.add(new GlobalEndpointInterceptor());
        }
.
.
.
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!