3

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.

VelNaga
  • 3,593
  • 6
  • 48
  • 82
  • Aren't you creating the interceptor yourself? (`new HeaderInterceptor()` in your code)? – Stephane Nicoll Sep 05 '16 at 15:12
  • Yes.I am creating the interceptor and I am adding this interceptor in the restTemplate setInterceptors method.It is working fine.Just i want to test whether we can dynamically pass value with @Value but it fails. – VelNaga Sep 05 '16 at 15:29
  • 1
    Like ritesh sugested, even though you have annotated HeaderInterceptor to be managed by spring(@Component). But by using the new operator ,the HeaderInterceptor instantiated is not spring managed and hence cannot access any context values. The interceptor works in your case because there wasnt anything stopping the flow (just the clientHeader property becoming null). Autowire the HeaderInterceptor and use that autowired attribute in the setInterceptor method of restTemplate. The value can be dynamically passed. – ameenhere Sep 06 '16 at 17:32

2 Answers2

4

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.

ritesh.garg
  • 3,725
  • 1
  • 15
  • 14
0

You can configure the @Bean for the interceptor which will ensure the @Autowired fields are indeed autowired and then use them to configure the client.

Before :

client.setInterceptors(new ClientInterceptor[] {new CustomInterceptor()});

After :

 @Bean
 public CustomInterceptor customInterceptor() {
    return new CustomInterceptor();
 }

 //in the client construction, set the interceptor as below.
 client.setInterceptors(new ClientInterceptor[] {customInterceptor()});

 
maheeka
  • 1,983
  • 1
  • 17
  • 25