Android的注解放心了头 [英] Android annotations REST set header
问题描述
我使用Android的注释,以及最近发现一个bug <一个href=\"http://stackoverflow.com/questions/13182519/spring-rest-template-usage-causes-eofexception\">Spring其余的模板使用造成EOFException类,我不知道如何使用注释来解决。我有POST请求:
I'm using Android annotations, and recently discovered a bug Spring Rest Template usage causes EOFException which I don't know how to fix using annotations. I have post request:
@Post("base/setItem.php")
Item setItem(Protocol protocol);
现在,我该如何设置标题
Now, how do I set header
headers.set("Connection", "Close");
这一要求?
谢谢!
推荐答案
有两种解决方案:
解决方案1
因为AA 3.0(仍在快照),你可以在<$使用拦截场C $ C> @Rest 注释和实现自定义的 ClientHtt prequestInterceptor
这将设置头到每个请求:
since AA 3.0 (still in snapshot), you can use interceptors field on @Rest
annotation and implement a custom ClientHttpRequestInterceptor
which will set headers to each request :
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
使用AA&LT; = 2.7.1,您应该创建一个 @EBean
注释与注射REST接口类。受此bean替换其他类中的所有注射REST接口。在这个新的bean,创建一个 @AfterInject
方法,它将检索 RestTemplate
实例和配置的解决方案,1拦截器:
With AA <= 2.7.1, you should create an @EBean
annotated class with your Rest interface injected. Replace all injected Rest interface on other classes by this bean. In this new bean, create an @AfterInject
method which will retrieve the RestTemplate
instance and configure the interceptor of solution 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());
}
}
这篇关于Android的注解放心了头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!