英文:
Is it possible to apply manually instantiated Java annotations on method parameters?
问题
在以下主题中:
- https://stackoverflow.com/questions/16299717/how-to-create-an-instance-of-an-annotation
- https://stackoverflow.com/questions/266903/create-annotation-instance-with-defaults-in-java
讨论了如何创建Java注解的实例。
我的问题是:拥有这样的注解实例,我可以用它做什么?特别地,我是否可以在方法的参数上以某种方式应用它?
动机:
我正在准备几个Azure函数,我不喜欢我需要重复多次类似以下代码的事实
@HttpTrigger(name = "req", methods = {HttpMethod.GET}, route= "/api/mypath", authLevel = AuthorizationLevel.ANONYMOUS)
所以我想我可以创建一个方法 HttpTrigger createHttpTrigger(HttpMethod httpMethod) { ... }
,然后代替
@FunctionName("MyFunction")
public HttpResponseMessage getModelNames(
@HttpTrigger(name = "req", methods = {HttpMethod.GET}, route= "api/mypath", authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
...
}
写类似于
@FunctionName("MyFunction")
public HttpResponseMessage getModelNames(
@createHttpTrigger(HttpMethod.GET) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
}
(但是当然这段代码是不正确的)
英文:
In the following topics:
- https://stackoverflow.com/questions/16299717/how-to-create-an-instance-of-an-annotation
- https://stackoverflow.com/questions/266903/create-annotation-instance-with-defaults-in-java
it is discussed how to create an instance of a Java annotation.
My question is: having such an annotation instance, what can I do with it? In particular, can I somehow apply it on a method's parameter?
Motivation:
I'm preparing several Azure Functions and I don't like the fact that I need to repeat many times code like
@HttpTrigger(name = "req", methods = {HttpMethod.GET}, route= "/api/mypath", authLevel = AuthorizationLevel.ANONYMOUS)
So I thought I'd create a method HttpTrigger createHttpTrigger(HttpMethod httpMethod) { ... }
and then instead of
@FunctionName("MyFunction")
public HttpResponseMessage getModelNames(
@HttpTrigger(name = "req", methods = {HttpMethod.GET}, route= "api/mypath", authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
...
}
write something like
@FunctionName("MyFunction")
public HttpResponseMessage getModelNames(
@createHttpTrigger(HttpMethod.GET) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
}
(but of course this code is incorrect)
答案1
得分: 1
我查看了HttpTrigger源代码,并发现我只需要向注释的属性添加一些默认值。
```java
package com.microsoft.azure.functions.annotation;
import com.microsoft.azure.functions.HttpMethod;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface HttpTrigger {
String name();
String dataType() default "";
String route() default "";
HttpMethod[] methods() default {};
AuthorizationLevel authLevel() default AuthorizationLevel.FUNCTION;
}
创建一个新的注释。
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface CreateHttpTrigger {
String name() default "req";
String dataType() default "";
String route() default "/api/mypath";
HttpMethod[] methods() default {};
AuthorizationLevel authLevel() default AuthorizationLevel.ANONYMOUS;
}
代码可以重写如下。
@FunctionName("HttpTrigger-Java")
public HttpResponseMessage run(
@CreateHttpTrigger(methods = {HttpMethod.GET}) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
但当我运行它时,显示以下错误消息:
"HttpTrigger-Java" 函数错误:必须声明至少一个绑定。
我尝试了几种方法,但没有一种使其成功运行,所以我在官方文档中检查了有关绑定和触发的信息。
从上表中,我推断自定义的 CreateHttpTrigger
注释是 Azure 函数不支持的绑定。
总之,根据我推测,你的想法可能无法实现。如果你有更好的想法,欢迎告诉我。我愿意继续讨论这个问题。
<details>
<summary>英文:</summary>
I looked at the HttpTrigger source code and found that I just needed to add some default values to the properties of the annotation.
package com.microsoft.azure.functions.annotation;
import com.microsoft.azure.functions.HttpMethod;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface HttpTrigger {
String name();
String dataType() default "";
String route() default "";
HttpMethod[] methods() default {};
AuthorizationLevel authLevel() default AuthorizationLevel.FUNCTION;
}
Create a new annotation.
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface CreateHttpTrigger {
String name() default "req";
String dataType() default "";
String route() default "/api/mypath";
HttpMethod[] methods() default {};
AuthorizationLevel authLevel() default AuthorizationLevel.ANONYMOUS;
}
The code can be rewritten like this.
@FunctionName("HttpTrigger-Java")
public HttpResponseMessage run(
@CreateHttpTrigger(methods = {HttpMethod.GET}) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
But when I run it, it shows the following error message:
>The 'HttpTrigger-Java' function is in error: At least one binding must be declared.
I tried several methods but none of them made it run successfully, so I checked the official [documentation][1] on binding and triggering.
[![enter image description here][2]][2]
From the above table, I infer that the custom `CreateHttpTrigger` annotation is a binding that is not supported by Azure function.
In summary, I guess your idea cannot be realized. If you have a better idea, you can tell me. I am willing to continue discussing this issue.
[1]: https://learn.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings#supported-bindings
[2]: https://i.stack.imgur.com/jvufu.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论