Fetch the repository succeeded.
This action will force synchronization from 捣鼓/forest, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
用过Spring MVC的朋友一定对spring的拦截器并不陌生,Forest也同样支持针对Forest请求的拦截器。
如果你想在很多个请求发送之前或之后做一些事情(如下日志、计数等等),拦截器就能帮你做到这些事情。
定义一个拦截器需要实现com.dtflys.forest.interceptor接口
public class SimpleInterceptor implements Interceptor<String> {
private final static Logger log = LoggerFactory.getLogger(SimpleInterceptor.class);
/**
* 该方法在请求发送之前被调用, 若返回false则不会继续发送请求
*/
@Override
public boolean beforeExecute(ForestRequest request) {
log.info("invoke Simple beforeExecute");
return true;
}
/**
* 该方法在请求成功响应时被调用
*/
@Override
public void onSuccess(String data, ForestRequest request, ForestResponse response) {
log.info("invoke Simple onSuccess");
}
/**
* 该方法在请求发送失败时被调用
*/
@Override
public void onError(ForestRuntimeException ex, ForestRequest request, ForestResponse response) {
log.info("invoke Simple onError");
}
/**
* 该方法在请求发送之后被调用
*/
@Override
public void afterExecute(ForestRequest request, ForestResponse response) {
log.info("invoke Simple afterExecute");
}
}
Interceptor接口带有一个泛型参数,其表示的是请求响应后返回的数据类型。 Interceptor即代表返回的数据类型为String。
需要调用拦截器的地方,只需要在该方法的@Request注解中设置interceptor属性即可。
public interface SimpleClient {
@Request(
url = "http://localhost:8080/hello/user?username=foo",
headers = {"Accept:text/plan"},
interceptor = SimpleInterceptor.class
)
String simple();
}
Sign in for post a comment
Comment ( 0 )