9.2.1 Functions as Web Applications

To run your function as a web application as described in this section, you will need the function-web dependency on your classpath. For example, in build.gradle

  1. implementation("io.micronaut:micronaut-http-server-netty")
  1. <dependency>
  2. <groupId>io.micronaut</groupId>
  3. <artifactId>micronaut-http-server-netty</artifactId>
  4. </dependency>
  1. implementation("io.micronaut:micronaut-function-web")
  1. <dependency>
  2. <groupId>io.micronaut</groupId>
  3. <artifactId>micronaut-function-web</artifactId>
  4. </dependency>
1In order to run the function as a web application, you will need an HTTP server, such as the http-server-netty dependency

Once the dependencies have been added to the project, you can run the function via an Application class.

  1. import io.micronaut.runtime.Micronaut;
  2. public class Application {
  3. public static void main(String[] args) {
  4. Micronaut.run(Application.class);
  5. }
  6. }
  1. import io.micronaut.runtime.Micronaut
  2. class Application {
  3. static void main(String... args) {
  4. Micronaut.run Application.class
  5. }
  6. }
  1. import io.micronaut.runtime.Micronaut
  2. object Application {
  3. @JvmStatic
  4. fun main(args: Array<String>) {
  5. Micronaut.run(Application.javaClass)
  6. }
  7. }

You can now make requests against the function with a REST client.

  1. $ curl -X GET http://localhost:8080/hello

The URI mapped to is defined by the either the value of the @FunctionBean annotation for Java or, in the case of Groovy, the name of the function defined in the function script. The following tables summarizes the convention:

Table 1. Function URI Mapping with @FunctionBean
AnnotationURI

@FunctionBean(“hello”)

/hello

@FunctionBean(“helloWorld”)

/helloWorld

@FunctionBean(“hello-world”)

/hello-world

Table 2. Function URI Mapping with Groovy Function Script
Method NameURI

hello

/hello

helloWorld

/hello-world

Functions that only return a value are mapped to HTTP GET requests, whilst functions that accept an input require an HTTP POST.

In addition, the function will be registered by the configured Service Discovery provider, and be made accessible to clients via the @FunctionClient annotation.

For further information on the use of @FunctionClient, please see Calling Functions.