Java 命名/可选参数使用注释?

发布于 2024-11-18 17:15:03 字数 520 浏览 1 评论 0原文

在 RESTeasy 中,这...

@GET
@Path("request")
public String requestJson(@QueryParam("arg1") @DefaultValue("") String arg1,
                          @QueryParam("arg2") @DefaultValue("0") Integer arg2);

...允许您定义方法签名中定义的参数的任何子集。当然,可以在任何方法签名上使用相同的模式,如下所示:

@Method
public String requestJson(@OptionalParameter("arg1") @DefaultValue("") String arg1,
                          @OptionalParameter("arg2") @DefaultValue("0") Integer arg2);

是否可以这样做?如果是这样怎么办?

In RESTeasy this...

@GET
@Path("request")
public String requestJson(@QueryParam("arg1") @DefaultValue("") String arg1,
                          @QueryParam("arg2") @DefaultValue("0") Integer arg2);

...allows you to define any subset of parameters defined in the method signature. Surely it is then possible to use this same pattern on any method signature something like this:

@Method
public String requestJson(@OptionalParameter("arg1") @DefaultValue("") String arg1,
                          @OptionalParameter("arg2") @DefaultValue("0") Integer arg2);

Is it possible to do this? If so how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝情姑娘 2024-11-25 17:15:03

RESTEasy 示例中的注释是可能的,因为该对象是由 RESTEasy 框架管理的。它可以检查方法并根据这些注释确定如何调用它。可以创建您想要的任何注释,关键是调用该方法的代码需要了解注释以及如何处理它们。

The annotations in the RESTEasy example are possible due to the fact that the object is managed by the RESTEasy framework. It can inspect the method and determine how to invoke it based on those annotations. It is possible to create any annotations you'd want, the key is that the code calling the method needs to know about the annotations and how to process them.

忆离笙 2024-11-25 17:15:03

首先,@laz说的是完全正确的。这在 RESTEasy 中是可能的,因为 RESTEasy 正在管理对您的类的调用。

我认为这里相关的问题是您期望 requestJson() 的调用是什么样子。一些示例:requestJson(1)requestJson("String")requestJson(null, 1)requestJson( “字符串”,空)

我可以想到五种方法来处理这个问题:

  1. 最好的方法):自己手动编写变体方法或设置 IDE 来生成它们。

  2. 如果该值没有默认值,则让调用者有责任将其设置为默认值(即调用者确定“我正在调用一个具有”的方法

  3. 拦截对 requestJson() 的调用并根据需要插入缺失的值(这会进入一些混乱的反射或运行时代码Generation voodoo;这是可能的,但它不会很简单——基本上你正在构建自己的 AOP 库,它将管理对 requestJson() 的所有调用。

  4. 插入编译器/构建链以生成所需的方法,即:

    public String requestJson() { requestJson("", 0); }
    公共字符串 requestJson(String arg1) { requestJson(arg1, 0); }
    公共字符串 requestJson(Int arg2) { requestJson("", arg2); }
    公共字符串 requestJson(String arg1, Int arg2) {...}
    

    (这类似于此处讨论的内容)

  5. 添加代码以确定缺失的内容值到 requestJson 实现的顶部。这只会启用像 requestJson(null, 1) 这样的东西,而且它可能不是您正在寻找的东西(因为它需要在 requestJson() 中进行额外的处理)。可以将样板提取到库中。

First of all, what @laz said was exactly right. This is possible in RESTEasy because RESTEasy is managing the calls to your class.

I think the pertinent question here is what you would expect an invocation of requestJson() to look like. Some examples: requestJson(1) and requestJson("String") versus something like requestJson(null, 1) and requestJson("String", null).

There are five ways I can think of to handle this:

  1. The least automated way (but ultimately, best way): Write the variant methods yourself either by hand or set your IDE up to generate them.

  2. Make it the caller's responsibility to set the value to a default value if it doesn't have one (i.e. the caller determines "I'm calling a method which has a "

  3. Intercept the call to requestJson() and plugin the missing values as needed (this gets into some messy reflection or runtime code generation voodoo; it's possible, but it's not going to be simple -- basically you're building your own AOP library which will be managing all the calls to requestJson()).

  4. Plug-in to the compiler / build chain to generate the required methods, i.e.:

    public String requestJson() { requestJson("", 0); }
    public String requestJson(String arg1) { requestJson(arg1, 0); }
    public String requestJson(Int arg2) { requestJson("", arg2); }
    public String requestJson(String arg1, Int arg2) {...}
    

    (This is similar to what is discussed here)

  5. Add the code to determine the missing values to the top of your requestJson implementation. This would only enable things like requestJson(null, 1), and it probably not what you are looking for (as it requires extra handing in requestJson()). It may be possible to extract the boiler plate into a library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文