How do you set a custom HTTP header (not SOAP header) dynamically on the client side when using Spring-WS?
相关问题
- Angular RxJS mergeMap types
- 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
Actually, it is an updated version of the @Tomasz's answer, but provides a new Spring-WS API, Java 8 shortcuts, and cares about creating a
WebServiceMessageCallback
instance with a separate method.I believe it is more obvious and self-sufficient.
When using spring integration 3 and spring integration-ws, the following code can be used for handling the request:
The Interceptor can be connected to the outbound gateway in the following way:
Spring's webServiceTemplate.marshalSendAndReceive(request) method internally uses HttpComponentsMessageSender to send the SOAP message over the network and this further uses WebServiceConnection to make http connection with the server. All you have to do is to write your own custom HttpComponentsMessageSender and set the cookie inside postMethod.
Custome sender code:
Spring Configuration :
After this I simply get bean webServiceTemplate and call marshalSendAndReceive method. So every request will have its custom cookie set before making HTTP call.
config:
Example Method with java 1.8: How to add a HTTP header:
Explanation: Use the getWebServiceTemplate().marshalSendAndReceive as described for example here: https://spring.io/guides/gs/consuming-web-service/
First parameter is the URI, second is the object which shall be send with the request. As third Parameter you can add as function
where you override the
public void doWithMessage
. This method gets called before the request is sent. Within you can access the message and add a request Header throughThe following fragment has been tested with Spring 4.0. It appends a
WebServiceMessageCallback
to aorg.springframework.ws.client.core.WebServiceTemplate