我正在尝试在API访问资源方法之前对其进行清理。我已经决定最好的方法是创建过滤器并动态绑定它们。但是,@PreMatching
注释只有在我将它们设为全局时才有效。我的代码是下一个:
过滤器:
@PreMatching
public class FilterTest implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext crc) throws IOException {
UriBuilder ub = crc.getUriInfo().getRequestUriBuilder();
ub.replaceQueryParam("count", 20);
crc.setRequestUri(ub.build());
}
}
动态绑定类:
@Provider
public class DynamicTest implements DynamicFeature {
@Override
public void configure(ResourceInfo ri, FeatureContext fc) {
if (MyResourceClass.class.equals(ri.getResourceClass())){
fc.register(FilterTest.class);
}
}
}
泽西岛申请文件:
public class JerseyApplication extends ResourceConfig {
public JerseyApplication() {
...
register(DynamicTest.class);
}
}
错误:
HTTP 500 - java.lang.IllegalStateException: Method could be called only in pre-matching request filter.
我该怎么办?