Java JAX-RS中使用@Required注释的必需@QueryParam在ContainerRequestFilter中不起作用

Java JAX-RS中使用@Required注释的必需@QueryParam在ContainerRequestFilter中不起作用,java,rest,jax-rs,wildfly,Java,Rest,Jax Rs,Wildfly,我也遇到了同样的问题,并尝试了Zero3的解决方案(),但在我的例子中,参数.isAnnotationPresent(Required.class)始终返回false 这是我需要的注释: import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Retentio

我也遇到了同样的问题,并尝试了Zero3的解决方案(),但在我的例子中,
参数.isAnnotationPresent(Required.class)
始终返回
false

这是我需要的注释:

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Required {
    // This is just a marker annotation, so nothing in here.
}
我还使用
BeanParam
注释尝试了它,并相应地修改了过滤器,但结果相同-对于
isAnnotionPresen
,总是得到
null

我使用的是WildFly 9(RESTeasy),它会自动注册请求过滤器

我的REST资源如下所示:

@GET
@Path("/{type}/{id}")
public Response getAllByTypeAndId(@Required @BeanParam RequiredQueryParams requiredQueryParams,
                                  @Required @QueryParam("mandant") String mandant,
                                  @PathParam("type") String type,
                                  @PathParam("id") Long id) {
...doSomething...
}
public boolean equals(Object obj) {
    return (this == obj);
}
运行调试器会为parameter.declaredAnnotations在
BeanParam
的HashMap中显示两个条目:

0 interface my.annotations.Required -> @my.annotations.Required()
1 interface javax.ws.rs.BeanParam -> @javax.ws.rs.BeanParam()
0 interface my.annotations.Required -> @my.annotations.Required()
1 interface javax.ws.rs.QueryParam -> @javax.ws.rs.QueryParam(value=mandant)
对于
查询参数

0 interface my.annotations.Required -> @my.annotations.Required()
1 interface javax.ws.rs.BeanParam -> @javax.ws.rs.BeanParam()
0 interface my.annotations.Required -> @my.annotations.Required()
1 interface javax.ws.rs.QueryParam -> @javax.ws.rs.QueryParam(value=mandant)

任何提示欢迎-谢谢

作为非工作
参数.isAnnotationPresent(Required.class)
中的
ContainerRequestFilter
的变通方法,我现在使用此方法:

private boolean isRequired(Parameter parameter) {
    for (Annotation annotation : parameter.getDeclaredAnnotations()) {
        if (Required.class.getName().equals(annotation.annotationType().getName())) {
            return true;
        }
    }
    return false;
}

我一直在想为什么IsAnnotionPresent()不起作用

根据你问题中的信息和评论中的补充信息,我想我大致了解了这里发生的事情。让我们从原始版本开始:

parameter.isAnnotationPresent(Required.class)始终返回
false


现在让我们来看看<代码>注释元素-yasiNoDebug的实现(类看起来像bean验证)更适合这个用例。检查RestStAy(Wildfly的JAX RS实现)文档。最后一个选项是关于bean验证的部分,因为我正在处理一个多租户应用程序,其中有很多REST服务,每个服务都需要强制的“mandant”参数。我更喜欢Zero3解决方案()中描述的过滤器解决方案有趣。您在注释类型中做了一些有趣的事情吗?比如重写

equals()
hashCode()
?因为您是按注释类名进行比较的(当然这不是一种非常可靠的方法),所以我假设您在直接与
equals()进行比较时遇到了问题
?调试提示:尝试将
equals()
hashcode()
的结果打印到您正在比较的名称旁边,看看是否正确。您好,Zero3,我的注释与您的注释相同:
@Target(ElementType.PARAMETER)@Retention(RetentionPolicy.RUNTIME)@Documented public@interface Required{//这只是一个标记注释,所以这里没有任何内容。}
比较名称的
hashCode()
是相等的,所以在我的工作中应该是正确的;-)嗯。实际上,我的意思是你可以试着打印
Required.class.hashCode()
annotation.annotationType().hashcode()
Required.class.equals(annotation.annotationType())
。如果这些与预期的不一致/不正确,则可能是您遇到麻烦的原因。当然,另一个原因可能是您在端点Java文件中错误地导入了一个错误的必需注释(您没有在原始帖子中发布完整的代码示例,因此很难在这里获得完整的信息)。我在原始帖子中添加了
Required
的实现。这里是我的调试输出:
Required.class=interface at.luxbau.mis2.commons.annotation.Required
annotation.annotationType()=interface at.luxbau.mis2.commons.annotation.Required
Required.class.equals(annotationType())=false//false,因为hashCode()返回不同的值
Required.class.hashCode()=145058991
annotation.annotationType().hashCode()=446212579
Required.class.getName()=at.luxbau.mis2.commons.annotation.Required
annotation.annotationType().getName()=at.luxbau.mis2.commons.annotation.Required
感谢您的详细解释。关于您保证使用相同的类加载器的提示,我重构了我的应用程序,
isAnnotationPresent()
按预期工作!我会把你的评论作为这个问题的答案!