【问题标题】:Android annotations REST set headerAndroid 注释 REST 集标头
【发布时间】:2013-08-24 09:14:45
【问题描述】:

我正在使用 Android 注释,最近发现了一个错误 Spring Rest Template usage causes EOFException,我不知道如何使用注释来修复它。我有发帖请求:

@Post("base/setItem.php")
Item setItem(Protocol protocol);

现在,我如何设置标题

headers.set("Connection", "Close");

这个请求?

谢谢!

【问题讨论】:

    标签: android http-headers android-annotations


    【解决方案1】:

    两种解决方案:

    解决方案 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

    使用 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());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-17
      • 2010-11-08
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多