How do I get Jersey Test/Client to not fill in a default Accept header?

前端 未结 3 1175
挽巷
挽巷 2021-01-18 03:51

I\'m trying to handle a request with no Accept header in a particular way, but Jersey seems hell-bent on filling one in, no matter what I do, so it always looks

3条回答
  •  悲哀的现实
    2021-01-18 04:20

    I'm facing the same issue right now and after some research, I ended up finding the problem.

    WebResourceFactory.invoke(final Object proxy, final Method method, final Object[] {
        ...
        Invocation.Builder builder = newTarget.request()
                .headers(headers) // this resets all headers so do this first
                .accept(accepts); // if @Produces is defined, propagate values into Accept header; empty array is NO-OP
        ...
    }
    

    The request builder is automatically adding into the existing headers the media types specified in the API (@Produce). Sadly I did not find a way to disable that behaviour. So I'll try to extend the WebResourceFactory class (which is final) used to create my client and overrided that method invoke to not call the accept on the request builder.

    Be aware that I dont consider this as a solution but more as a workaround.

    [EDIT] Since WebResourceFactory is final, I have duplicated it's content and removed the accept call on the request builder. A little bit ugly, I know and hope someone will find a better way.

提交回复
热议问题