目前有一个未解决的问题:https ://github.com/excilys/androidannotations/issues/323
目前,唯一的方法是使用自定义 ClientHttpRequestInterceptor。这是一个小例子:
@EBean
public class CustomHeaderInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] data, ClientHttpRequestExecution execution) throws IOException {
request.getHeaders().add("myHeader", "value");
return execution.execute(request, data);
}
}
然后,您需要将其链接到 restTemplate,如下所示:
@EBean
public class MyService {
@RestService
RestClient restClient;
@Bean
MobileParametersInterceptor mobileParametersInterceptor;
@AfterInject
public void init() {
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add(mobileParametersInterceptor);
restClient.getRestTemplate().setInterceptors(interceptors);
}
}