1

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 = "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)

kdr
  • 93
  • 8
  • if you are writing custom annotations, then you can use reflection api to scan and modify your api/implementations accordingly. I was able to achieve some use cases where I had sort of requirement. – bananas Aug 19 '20 at 09:33
  • 1
    this can help https://stackoverflow.com/questions/27469305/groovy-how-to-call-annotated-methods?rq=1 – bananas Aug 19 '20 at 09:56

1 Answers1

1

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 on binding and triggering.

enter image description here

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.

Frank Borzage
  • 6,292
  • 1
  • 6
  • 19