Java 命名/可选参数使用注释?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
首先,@laz说的是完全正确的。这在 RESTEasy 中是可能的,因为 RESTEasy 正在管理对您的类的调用。
我认为这里相关的问题是您期望 requestJson() 的调用是什么样子。一些示例:
requestJson(1)
和requestJson("String")
与requestJson(null, 1)
和requestJson( “字符串”,空)
。我可以想到五种方法来处理这个问题:
如果该值没有默认值,则让调用者有责任将其设置为默认值(即调用者确定“我正在调用一个具有”的方法
拦截对 requestJson() 的调用并根据需要插入缺失的值(这会进入一些混乱的反射或运行时代码Generation voodoo;这是可能的,但它不会很简单——基本上你正在构建自己的 AOP 库,它将管理对 requestJson() 的所有调用。
插入编译器/构建链以生成所需的方法,即:
(这类似于此处讨论的内容)
添加代码以确定缺失的内容值到 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)
andrequestJson("String")
versus something likerequestJson(null, 1)
andrequestJson("String", null)
.There are five ways I can think of to handle this:
The least automated way (but ultimately, best way): Write the variant methods yourself either by hand or set your IDE up to generate them.
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 "
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()).
Plug-in to the compiler / build chain to generate the required methods, i.e.:
(This is similar to what is discussed here)
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.