How to disable chunking in cxf webservice on serve

2020-07-19 06:47发布

问题:

I need to disable chunking in cxf webservice on server-side as some clients need 'Content-Length' header in response. Now i can see 'Transfer-Encoding' is 'chunked' in server response and no 'Content-Length' header is sent.

I've found that chunkins can be disabled in spring's context like this:

<http-conf:conduit name="*.http-conduit">
    <http-conf:client ReceiveTimeout=“300000“ AllowChunking="false"/>
</http-conf:conduit>

Since i'm creating services programmatically like this:

// media service
Object mediaService = new MediaService();
System.out.println("Starting media service #1 ...");
EndpointImpl mediaEP = (EndpointImpl)Endpoint.create(mediaService);
mediaEP.publish("http://localhost:8081/onvif/media_service");

How can i do it?

回答1:

Actually, you can't easyly specified to not allow chuncking from server side. Indeed, it's a client pb! What I understand is that you have a client of your ws who can't modify his code to desactivate chunking?

You have to do that : write a CXF interceptor that would replace the servlets OutputStream in the message with a buffer of some sort (ByteArrayOutputStream or CachedOutputStream) at the beginning of the output chain and then at the end of the chain, use that to set the Content-Length header on the response and copy that data to the real output stream. Indeed, the content lenght will force the framework to not use the chunking.

I did it once before. I'll try to post you maybe tomorrow a code of such interceptor.