Hi,

Try to use code bellow, it should work:

@WebService(endpointInterface = "demo.hw.server.HelloWorld", serviceName = 
"HelloWorld")
public class HelloWorldImpl implements HelloWorld {
        @Resource
        WebServiceContext context;

        public String sayHi(String text) {
                System.out.println("sayHi called");

                try {
                        List<Header> headers = new ArrayList<Header>();
                        Header dummyHeader;
                        dummyHeader = new Header(new 
QName("uri:org.apache.cxf", "dummy"),
                                        "decapitated", new 
JAXBDataBinding(String.class));
                        headers.add(dummyHeader);

                        // server side:
                        context.getMessageContext().put(Header.HEADER_LIST, 
headers);

                        return "Hello " + text;
                } catch (JAXBException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        throw new RuntimeException(e);
                }
        }
}

Regards,
Andrei.

> -----Original Message-----
> From: gcsaba2ms [mailto:[email protected]]
> Sent: Freitag, 22. Februar 2013 15:45
> To: [email protected]
> Subject: How to add custom headers to SOAP response? The code from the
> FAQ won't work!
> 
> Hello,
> 
> I have a legacy SOAP application which returns a random number of SOAP
> headers. I'm trying to implement this now with JAX-WS and CXF.
> 
> I did search the internet and found that the solution for that would be the
> following code:
> 
> =====
> 
> List<Header> headers = new ArrayList<Header>(); Header dummyHeader =
> new Header(new QName("uri:org.apache.cxf", "dummy"), "decapitated",
>                                 new JAXBDataBinding(String.class));
> headers.add(dummyHeader);
> 
> //server side:
> context.getMessageContext().put(Header.HEADER_LIST, headers);
> 
> =====
> 
> I've tried many variations of that code: without JAXBDataBinding, adding
> Element instead of String, using SoapHeader instead of Header... it doesn't
> work. Every time I get a SOAP response without a header, like this:
> 
> <soap:Envelope
> xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/";>
>   <soap:Body>
>       ...
> 
> 
> If I DON'T include that code above and instead I add headers with
> @WebParam(header=true, then it will work, the header will get added.
> However I can only add a fixed number of headers that way, which is not
> what I want.
> 
> Any idea why the code above won't work? I don't think I'm doing anything
> special... I have a @WebService class, it's exposed through TCP.
> 
> I've added an interceptor which extends AbstractOutDatabindingInterceptor,
> so it gets called in the final phases. I've checked and at that point the 
> header
> list has size = 0
> 
> 
> 
> 
> --
> View this message in context: http://cxf.547215.n5.nabble.com/How-to-add-
> custom-headers-to-SOAP-response-The-code-from-the-FAQ-won-t-work-
> tp5723581.html
> Sent from the cxf-user mailing list archive at Nabble.com.

Reply via email to