java 我应该在 JAX-RS 中使用 @QueryParam 还是 @BeanParam?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38216487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:13:20  来源:igfitidea点击:

Should I use @QueryParam or @BeanParam in JAX-RS?

javajax-rs

提问by user1539343

I am thinking of two options for handling query/request parameters:

我正在考虑处理查询/请求参数的两个选项:

  1. Map individual parameters to corresponding the method parameters:
  1. 将各个参数映射到相应的方法参数:
@GET
public String blah(@QueryParam("testParam") String testParam) {

}
  1. Map all parameters to the properties of a Java bean:
  1. 将所有参数映射到 Java bean 的属性:
@GET
public String blah(@BeanParam RequestParamBean bean) {

}

The second option seems more attractive as it allows the validation logic of input query parameters to be moved and decoupled from the blahmethod whose core responsibility should be to process and delegating the validation to a validator should high degree of decoupling (and also SOLID principle, right?).

第二个选项似乎更有吸引力,因为它允许输入查询参数的验证逻辑移动并与blah方法解耦,该方法的核心职责应该是处理并将验证委托给验证器应该高度解耦(以及 SOLID 原则,对?)。

However, most of the examples I see (in fact, the existing project I am working on) use only the first option. I am wondering if is there any reason why the second option is not widely used? Are there any pitfalls? Is this an anti-pattern? Is this against any best practice?

但是,我看到的大多数示例(实际上是我正在处理的现有项目)仅使用第一个选项。我想知道是否有任何理由不广泛使用第二个选项?有什么陷阱吗?这是一种反模式吗?这是否违反任何最佳实践?

回答by cassiomolin

The @BeanParamannotation was introduced in JAX-RS 2.0 as a parameter aggregator(which means it cannot be used in JAX-RS 1.0).

@BeanParam注解是在 JAX-RS 2.0 中作为参数聚合器引入的(这意味着它不能在 JAX-RS 1.0 中使用)。



The idea behind the @BeanParamannotation is to have a Java class to aggregate parameters annotated with @XxxParamannotations. The following @XxxParamannotations can be used to annotate the fields of a parameter aggregator class:

@BeanParam注释背后的想法是有一个 Java 类来聚合用@XxxParam注释注释的参数。以下@XxxParam注释可用于注释参数聚合器类的字段:

Besides fields annotated the @XxxParamannotations, the parameter aggregator class can have fields annotated with the @Contextannotation. For a list of types that can be injected with the @Contextannotation, check this answer.

除了注释注释的字段之外@XxxParam,参数聚合器类可以具有用@Context注释注释的字段。有关可以使用@Context注释注入的类型列表,请查看此答案



I believe it's just a matter of convenience and preference of the developers. In many situations, a class to aggregate parameters is not necessary. The use of the @XxxParamannotations in method parameters is very handy.

我相信这只是开发人员的方便和偏好问题。在许多情况下,不需要聚合参数的类。@XxxParam在方法参数中使用注解非常方便。

But when you need to reuse parametersin different methods or the method has many parametersannotated with @XxxParamannotations, go for the @BeanParamapproach.

但是当你需要在不同的方法中重用参数或者方法中有很多带有@XxxParam注解的参数时,请使用这种@BeanParam方法。



In your question, you mentioned the SOLID principle. But don't forget the KISS principle:)

在您的问题中,您提到了SOLID 原则。但不要忘记KISS 原则:)

Start with the @XxxParamannotations in your method parameters and don't overuse the @BeanParamannotation trying to solve a problem you don't have. You always can refactor your code to create a parameter aggregator class if you need it.

@XxxParam方法参数中的注释开始,不要过度使用@BeanParam注释来解决您没有的问题。如果需要,您始终可以重构代码以创建参数聚合器类。