Support ordered interceptors in RestTemplate

This commit sorts `ClientHttpRequestInterceptor`s when those are set in
`InterceptingHttpAccessor` (which `RestTemplate` extends from).

Interceptors can now be annotated with `@Order` or implements `Ordered`
to reflect their order of execution for each request.

Issue: SPR-13971
This commit is contained in:
Brian Clozel
2016-08-25 17:14:09 +02:00
parent ab7107c4c5
commit f93cb2f539
2 changed files with 93 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ package org.springframework.http.client.support;
import java.util.ArrayList;
import java.util.List;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
@@ -40,6 +41,7 @@ public abstract class InterceptingHttpAccessor extends HttpAccessor {
* Sets the request interceptors that this accessor should use.
*/
public void setInterceptors(List<ClientHttpRequestInterceptor> interceptors) {
AnnotationAwareOrderComparator.sort(interceptors);
this.interceptors = interceptors;
}