【发布时间】:2016-12-03 01:43:02
【问题描述】:
我有同样的问题并尝试了 Zero3 的解决方案 (Required @QueryParam in JAX-RS (and what to do in their absence)),但在我的情况下,parameter.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...
}
运行调试器会为 parameter.declaredAnnotations 显示 HashMap 中BeanParam 的两个条目:
0 interface my.annotations.Required -> @my.annotations.Required()
1 interface javax.ws.rs.BeanParam -> @javax.ws.rs.BeanParam()
对于QueryParam:
0 interface my.annotations.Required -> @my.annotations.Required()
1 interface javax.ws.rs.QueryParam -> @javax.ws.rs.QueryParam(value=mandant)
欢迎任何提示 - 谢谢!
【问题讨论】:
-
似乎 bean 验证更适合这个用例。查看 RESTEasy(Wildfly 的 jax-rs 实现)文档。有一节是关于 bean 验证的
-
那将是最后一个选项,因为我正在开发一个多租户应用程序,其中我们有很多 REST 服务,并且每个服务都需要强制的“mandant”参数。我更喜欢 Zero3 的解决方案 (stackoverflow.com/questions/13968261/…) 中描述的过滤器解决方案