java中如何获取参数的注解?
我是一名新的 Java 程序员。以下是我的代码:
public void testSimple1(String lotteryName,
int useFrequence,
Date validityBegin,
Date validityEnd,
LotteryPasswdEnum lotteryPasswd,
LotteryExamineEnum lotteryExamine,
LotteryCarriageEnum lotteryCarriage,
@TestMapping(key = "id", csvFile = "lottyScope.csv") xxxxxxxx lotteryScope,
@TestMapping(key = "id", csvFile = "lotteryUseCondition.csv") xxxxxxxx lotteryUseCondition,
@TestMapping(key = "id", csvFile = "lotteryFee.csv") xxxxxxxx lotteryFee)
我想获取所有归档的注释。有些字段有注释,有些则没有。
我知道如何使用 method.getParameterAnnotations()
函数,但它只返回三个注释。
我不知道如何对应它们。
我期望得到以下结果:
lotteryName - none
useFrequence- none
validityBegin -none
validityEnd -none
lotteryPasswd -none
lotteryExamine-none
lotteryCarriage-none
lotteryScope - @TestMapping(key = "id", csvFile = "lottyScope.csv")
lotteryUseCondition - @TestMapping(key = "id", csvFile = "lotteryUseCondition.csv")
lotteryFee - @TestMapping(key = "id", csvFile = "lotteryFee.csv")
I'm a new Java programmer. Following is my code:
public void testSimple1(String lotteryName,
int useFrequence,
Date validityBegin,
Date validityEnd,
LotteryPasswdEnum lotteryPasswd,
LotteryExamineEnum lotteryExamine,
LotteryCarriageEnum lotteryCarriage,
@TestMapping(key = "id", csvFile = "lottyScope.csv") xxxxxxxx lotteryScope,
@TestMapping(key = "id", csvFile = "lotteryUseCondition.csv") xxxxxxxx lotteryUseCondition,
@TestMapping(key = "id", csvFile = "lotteryFee.csv") xxxxxxxx lotteryFee)
I want to get all filed's annotations. Some fields are annotated and some ain't.
I know how to use method.getParameterAnnotations()
function, but it just returns three annotations.
I don't know how to correspond them.
I expect the following result:
lotteryName - none
useFrequence- none
validityBegin -none
validityEnd -none
lotteryPasswd -none
lotteryExamine-none
lotteryCarriage-none
lotteryScope - @TestMapping(key = "id", csvFile = "lottyScope.csv")
lotteryUseCondition - @TestMapping(key = "id", csvFile = "lotteryUseCondition.csv")
lotteryFee - @TestMapping(key = "id", csvFile = "lotteryFee.csv")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getParameterAnnotations
为每个参数返回一个数组,对于任何没有任何注释的参数使用空数组。例如:这给出了输出:
这表明第一个参数没有注释,第二个参数有一个注释。 (当然,注释本身将位于第二个数组中。)
这看起来正是您想要的,所以我对您的说法感到困惑
getParameterAnnotations
“仅返回 3 个注释” - 它将返回数组的数组。也许您正在以某种方式展平返回的数组?getParameterAnnotations
returns one array per parameter, using an empty array for any parameter which doesn't have any annotations. For example:This gives output:
That shows that the first parameter has no annotations, and the second parameter has one annotation. (The annotation itself would be in the second array, of course.)
That looks like exactly what you want, so I'm confused by your claim that
getParameterAnnotations
"only returns 3 annotations" - it will return an array of arrays. Perhaps you're somehow flattening the returned array?