可以在方法参数上手动应用Java注解吗?

huangapple go评论85阅读模式
英文:

Is it possible to apply manually instantiated Java annotations on method parameters?

问题

在以下主题中:

讨论了如何创建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:

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 = &quot;req&quot;, methods = {HttpMethod.GET}, route= &quot;/api/mypath&quot;, authLevel = AuthorizationLevel.ANONYMOUS)

So I thought I'd create a method HttpTrigger createHttpTrigger(HttpMethod httpMethod) { ... } and then instead of

@FunctionName(&quot;MyFunction&quot;)
public HttpResponseMessage getModelNames(
   @HttpTrigger(name = &quot;req&quot;, methods = {HttpMethod.GET}, route= &quot;api/mypath&quot;, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage&lt;Optional&lt;String&gt;&gt; request, 
   final ExecutionContext context) {
...
}

write something like

@FunctionName(&quot;MyFunction&quot;)
public HttpResponseMessage getModelNames(
   @createHttpTrigger(HttpMethod.GET) HttpRequestMessage&lt;Optional&lt;String&gt;&gt; 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" 函数错误:必须声明至少一个绑定。

我尝试了几种方法,但没有一种使其成功运行,所以我在官方文档中检查了有关绑定和触发的信息。

可以在方法参数上手动应用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 &quot;&quot;;

String route() default &quot;&quot;;

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 &quot;&quot;;

String route() default &quot;/api/mypath&quot;;

HttpMethod[] methods() default {};

AuthorizationLevel authLevel() default AuthorizationLevel.ANONYMOUS;

}


The code can be rewritten like this.

@FunctionName(&quot;HttpTrigger-Java&quot;)
public HttpResponseMessage run(
        @CreateHttpTrigger(methods = {HttpMethod.GET}) HttpRequestMessage&lt;Optional&lt;String&gt;&gt; request,
        final ExecutionContext context) {
But when I run it, it shows the following error message:

&gt;The &#39;HttpTrigger-Java&#39; 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>



huangapple
  • 本文由 发表于 2020年8月4日 21:10:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63247630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定