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
implementation("io.micronaut:micronaut-http-server-netty")
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-server-netty</artifactId>
</dependency>
implementation("io.micronaut:micronaut-function-web")
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-function-web</artifactId>
</dependency>
1 | In 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.
import io.micronaut.runtime.Micronaut;
public class Application {
public static void main(String[] args) {
Micronaut.run(Application.class);
}
}
import io.micronaut.runtime.Micronaut
class Application {
static void main(String... args) {
Micronaut.run Application.class
}
}
import io.micronaut.runtime.Micronaut
object Application {
@JvmStatic
fun main(args: Array<String>) {
Micronaut.run(Application.javaClass)
}
}
You can now make requests against the function with a REST client.
$ 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:
Annotation | URI |
---|---|
|
|
|
|
|
|
Method Name | URI |
---|---|
|
|
|
|
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. |