兩個解決方案:
解決方案1
因爲AA 3.0(仍在快照),你可以在@Rest
註釋使用interceptors領域,實現自定義ClientHttpRequestInterceptor
,將設置頭到每個請求:
public class HeadersRequestInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
request.getHeaders().set("Connection", "Close");
return execution.execute(request, body);
}
}
溶液2
With AA < = 2.7.1,你應該創建一個@EBean
帶註釋的Rest接口的註釋類。用這個bean替換其他類中的所有注入的Rest接口。在這個新的bean,創建一個@AfterInject
方法,將檢索RestTemplate
實例和配置的解決方案1攔截:
RestClient.java:
@Rest(...)
public interface RestClient {
@Post("base/setItem.php")
Item setItem(Protocol protocol);
RestTemplate getRestTemplate();
}
RestClientProxy.java:
@EBean
public class RestClientProxy {
@RestService
RestClient restClient;
@AfterInject
void init() {
RestTemplate restTemplate = restClient.getRestTemplate();
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
interceptors.add(new HeadersRequestInterceptor());
}
}