我们必须在项目中支持JBoss AS 7.1.3和Wildfly . 为了提供这种可能性,我们有两个具有不同依赖关系和版本的maven配置文件,它们在AS中提供 . 一切正常,但最近我们遇到了JBoss的问题,与Resteasy有关 .
我们的REST服务使用 @Consumes(MediaType.APPLICATION_JSON)
注释 .
但是,如果我们如果 PUT
请求没有设置 Content-Type
标头字段,我们会收到 404
状态的响应 . 我们期望用 415 Unsupported Media Type
响应,所以我们写了拦截器来检查 MediaType
并且如果它没有设置则抛出 UnsupportedMediaTypeException
.
在Wildfly这个问题是固定的,所以我们不需要这个拦截器 .
主要问题是Resteasy的主要版本在Wildfly - 3.0.8.Final中有所不同(在JBoss 7.1.3中它是2.3.3.Final)并且存在一些不兼容的变化 .
例如 . 我们的拦截器实现 org.jboss.resteasy.spi.interception.PreProcessInterceptor
,在Resteasy 3.0.8中标记为Deprecated,并且自Resteasy 2.3.3以来它的签名 preProcess
方法已经改变 .
2.3.3的签名 PreProcessInterceptor.preProcess
:
ServerResponse preProcess(HttpRequest request, ResourceMethod method) throws Failure, WebApplicationException;
和3.0.8:
ServerResponse preProcess(HttpRequest request, ResourceMethodInvoker method) throws Failure, WebApplicationException;
所以我们的拦截器甚至不会为Wildfly编译 .
问题是:如何解决这个问题,使代码可以编译为JBoss AS 7.1.3 / Wildfly并避免在Wildfly中使用这个拦截器?
通过注释注册的拦截器:
@Provider
@ServerInterceptor
public class MyInterceptor implements PreProcessInterceptor
附:我们有互操作模块来提供这些平台中不同的类,例如有不同的包名称 .
1 回答
最后,我想出了以下解决方案:
创建我自己的界面
public interface MyPreProcessInterceptor extends PreProcessInterceptor { //method signature for Restesy 2.3.3 ServerResponse preProcess(HttpRequest request, ResourceMethod method) throws Failure, WebApplicationException;
//method signature for Restesy 3.0.8 ServerResponse preProcess(HttpRequest request, ResourceMethodInvoker method) throws Failure, WebApplicationException;
}
在Wildfly的互操作中创建
ResourceMethod
类在JBoss AS 7.1.3的互操作中创建
ResourceMethodInvoker
类修改
MyInterceptor
以实现新界面MyPreProcessInterceptor
仅在Restesy 2.3.3的方法中实现必要的逻辑,对于3.0.8只实现
return null;
所以现在它现在可以编译并适用于这两个平台 .