我有一个 Resteasy 应用程序,它使用 Spring 并包含 ContainerRequestFilter
和 ContainerResponseFilter
实现,并用 @Provider
注释。该应用程序使用的是 Resteasy 的 3.0-beta-6
版本。
当这些过滤器被添加到 web.xml 中的 resteasy.providers
上下文参数时,它们将按预期工作,如下所示:
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>foo.filter.LoggingRequestFilter,
foo.filter.LoggingResponseFilter</paramvalue>
</context-param>
如果我从这里删除过滤器,它们将不再被调用。
我假设这些提供者在使用 org.jboss.resteasy.plugins.spring.SpringContextLoaderListener
时会自动向 Resteasy 注册。对我来说奇怪的是,它在以前版本的 Resteasy 中适用于 PreProcessInterceptor
实现,并且在 v3 中仍然有效,但 Filters 和 ExceptionMappers 不会自动注册。
问题
- 如果类使用
@Provider
注释并由 Spring 扫描,为什么resteasy.providers
上下文参数是必需的? - 是否有一种编程方式可以在运行时设置这些提供程序?
最佳答案
为了让 Spring 扫描供应商,我必须在我的 Spring Java 配置类上将 includeFilters
参数添加到 @ComponentScan
。
@ComponentScan(value = "com.foo",
includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, value = Provider.class))
你也可以只用 @Component
和 @Provider
来注释它们,当使用 Resteasy 的 SpringContextLoaderListener
时,Spring 将确保它们被 Resteasy 检测到>.
关于java - 为什么在使用带@Provider 注解的 Spring 时仍然需要 resteasy.providers?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16892533/