有没有办法使用 PostProcessInterceptor 仅过滤特定类型的 ServerResponse?

Is there a way to filter only a particular type of ServerResponse using PostProcessInterceptor?

PostProcessInterceptor 将拦截所有类型的响应。但就我而言,我有两类回应。我必须截取一种,而另一种不受影响(作为普通的 json 字符串结果)。

有没有办法跳过对其他响应类型的拦截。可能是某种标记?或者有什么不同的方法吗?

对于 JAX-RS 2,您可以使用 name binding for a ContainerResponseFilter。定义注解:

@NameBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Special {}

并标记过滤器:

@Provider
@Special
public class SpecialFilter implements ContainerResponseFilter {}

仅当资源 class 或方法也被注释 @Special 时才会执行过滤器。

如果您需要使用已弃用的 PostProcessInterceptor,则不能使用名称绑定。但是 postProcess 方法中的一个简单的 if 会做同样的事情。