I am using spring-boot-1.3.3.I want to intercept Rest template and i am able to intercept it but i am unable to access the application.properties.It always returns null.
package com.sample.filter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class HeaderInterceptor implements ClientHttpRequestInterceptor{
private static final String CLIENT_HEADER = "x-client";
@Value("${clientHeader}")
private String ClientHeader;
@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpHeaders headers = httpRequest.getHeaders();
headers.add(CLIENT_HEADER, ClientHeader);
return execution.execute(httpRequest, body);
}
}
I am always getting "clientHeader" key as null.
Any help should be appreciable.
I see you mentioned in your comment that you are creating the interceptor yourself in the code by new
keyword. In order to use an spring context instance of HeaderInterceptor, you need to autowire it in your code. Then only, it will have visibility of spring managed properties.