If you are using Spring HTTP Remoting to communicate between your applications, sometimes you might want to pass certain custom HTTP header elements in the requests. Following approach would help you to solve this problem:
Extend SimpleHttpInvokerRequestExecutor
Extend SimpleHttpInvokerRequestExecutor with a custom implementation. In this implemenetation over-ride prepareConnection() method to set additional HTTP header elements:
public class MyHttpInvokerRequestExecutor extends SimpleHttpInvokerRequestExecutor {
@Override
protected void prepareConnection(HttpURLConnection con, int contentLength) throws IOException {
con.addRequestProperty(“header-element-key”, “header-element-value”);
super.prepareConnection(con, contentLength);
}
}
Inject in to HttpInvokerProxyFactoryBean
Now besides all othe regular fields to HttpInvokerProxyFactoryBean, inject this new MyHttpInvokerRequestExecutor
class=”org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean”>
:
:
class=”com.tier1app.remoting.httpinvoker.MyHttpInvokerRequestExecutor” />
Now “header-element-key” will be passed in and each & every HTTP requests. Enjoy 🙂
Leave a Reply