我正在使用 Android 注释,最近发现了一个错误Spring Rest 模板使用导致 EOFException https://stackoverflow.com/questions/13182519/spring-rest-template-usage-causes-eofexception我不知道如何使用注释来修复。我有帖子请求:
@Post("base/setItem.php")
Item setItem(Protocol protocol);
现在,我如何设置标题
headers.set("Connection", "Close");
对这个要求?
Thanks!
两种解决方案:
解决方案1
从 AA 3.0(仍在快照中)开始,您可以使用拦截器 https://github.com/excilys/androidannotations/wiki/Rest-API#rest场上@Rest
注释并实现自定义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
当 AA @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());
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)