Having created a java web service client using wsimport on a wsdl, I need to set the Authorization header for each soap message embedded in an http request. Having generated a subclass of javax.xml.ws.Service, how can I append an http header to each outgoing request???
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
For the sake of completeness and to help others in similar situations, I'd like to illustrate the IMHO cleanest solution using the JAX-WS-handler-chain:
1) Sub-class your service-class (not the port-class) in a different (non-generated) package. Because the service-class (and its entire package) was likely generated from a WSDL, your changes to the sub-class are not lost, when you update your service-class after a WSDL change.
2) Annotate your service-sub-class like this (import
javax.jws.HandlerChain
):3) Create a file called
HandlerChain.xml
in the same package as your service-sub-class, i.e. next toMyService
with the following content:You may add multiple
<handler>
elements, btw.And make sure that this file really ends up in your JAR! For example, when using Maven, you have to place it either in
${project}/src/main/resources/
(instead of${project}/src/main/java/
) or you have to change your build-configuration to include resources from thejava
-folder! I recommend the latter, because it's cumbersome to have a parallel package-structure in theresources
-folder, which is often forgotten during refactorings.4) Implement your
HttpHeaderExtensionSOAPHandler
-- similar to this:In my example above (and in my productive code) I obtain the data to be passed into the HTTP request headers from a
ThreadLocale
, i.e. my current thread's context. Since thisWebserviceContext
is my custom class, you'll need to implement your own way to access your data.when you're sending in Message mode, you can also pass MimeHeaders on SOAP Message, which will eventually translate into http headers, i.e:
You can pass a map with custom headers to the BindingProvider (I believe you can set the MessageContext.HTTP_REQUEST_HEADERS property). Try creating an Authorization header and passing it in.
Here is the code, based on Femi's answer.
It can be a little tricky to figure out. Works beautifully!