五、使用过滤器个性化定制
示例3:带自身过滤器(在声明式 HttpClient 体验中,非常有价值;方便为不同站点指定编码等过滤策略)
@NamiClient(url="http://localhost:8080/ComplexModelService/")
public interface IComplexModelService extends Filter{
//实际请求为:PUT http://localhost:8080/ComplexModelService/save
@NamiMapping("PUT")
void save(@NamiBody ComplexModel model);
//实际请求为:GET http://localhost:8080/ComplexModelService/api/1.0.1?modelId=xxx
@NamiMapping("GET api/1.0.1")
ComplexModel read(Integer modelId);
//自带个过滤器,过滤自己:) //要用 default 直接实现代码!!!
default Result doFilter(Invocation inv) throws Throwable{
inv.headers.put("Token", "Xxx");
inv.headers.put("TraceId", Utils.guid());
inv.config.setDecoder(SnackDecoder.instance);
inv.config.setEncoder(SnackEncoder.instance);
return inv.invoke();
}
}