title | description | services | documentationcenter | author | manager | keywords | ms.service | ms.devlang | ms.topic | ms.date | ms.author | ms.custom |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Create your first function in Azure with Java and Maven| Microsoft Docs |
Create and publish a simple HTTP triggered function to Azure with Java and Maven. |
functions |
na |
rloutlaw |
justhe |
azure functions, functions, event processing, compute, serverless architecture |
azure-functions |
java |
quickstart |
08/10/2018 |
routlaw, glenga |
mvc, devcenter |
[!INCLUDE functions-java-preview-note]
This quickstart guides through creating a serverless function project with Maven, testing it locally, and deploying it to Azure Functions. When you're done, you have a HTTP-triggered function app running in Azure.
[!INCLUDE quickstarts-free-trial-note]
To develop functions app with Java, you must have the following installed:
- Java Developer Kit, version 8.
- Apache Maven, version 3.0 or above.
- Azure CLI
Important
The JAVA_HOME environment variable must be set to the install location of the JDK to complete this quickstart.
The Azure Functions Core Tools provide a local development environment for writing, running, and debugging Azure Functions from the terminal or command prompt.
Install version 2 of the Core Tools on your local computer before continuing.
In an empty folder, run the following command to generate the Functions project from a Maven archetype.
mvn archetype:generate \
-DarchetypeGroupId=com.microsoft.azure \
-DarchetypeArtifactId=azure-functions-archetype
mvn archetype:generate ^
-DarchetypeGroupId=com.microsoft.azure ^
-DarchetypeArtifactId=azure-functions-archetype
Maven will ask you for values needed to finish generating the project. For groupId, artifactId, and version values, see the Maven naming conventions reference. The appName value must be unique across Azure, so Maven generates an app name based on the previously entered artifactId as a default. The packageName value determines the Java package for the generated function code.
The appRegion
value specifies which Azure region you want to run the deployed Function app in. You can get a list of region name values through the az account list-locations
command in the Azure CLI. The resourceGroup
value specifies which Azure resource group the function app will be created in.
The com.fabrikam.functions
and fabrikam-functions
identifiers below are used as an example and to make later steps in this quickstart easier to read. You are encouraged to supply your own values to Maven in this step.
Define value for property 'groupId': com.fabrikam.functions
Define value for property 'artifactId' : fabrikam-functions
Define value for property 'version' 1.0-SNAPSHOT :
Define value for property 'package': com.fabrikam.functions
Define value for property 'appName' fabrikam-functions-20170927220323382:
Define value for property 'appRegion' westus :
Define value for property 'resourceGroup' java-functions-group:
Confirm properties configuration: Y
Maven creates the project files in a new folder with a name of artifactId, in this example fabrikam-functions
. The ready to run generated code in the project is a simple HTTP triggered function that echoes the body of the request after a "Hello, " string.
public class Function {
@FunctionName("HttpTrigger-Java")
public HttpResponseMessage HttpTriggerJava(
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
// Parse query parameter
String query = request.getQueryParameters().get("name");
String name = request.getBody().orElse(query);
if (name == null) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build();
} else {
return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + name).build();
}
}
}
Change directory to the newly created project folder and build and run the function with Maven:
cd fabrikam-functions
mvn clean package
mvn azure-functions:run
Note
If you're experiencing this exception: javax.xml.bind.JAXBException
with Java 9, see the workaround on GitHub.
You see this output when the function is running locally on your system and ready to respond to HTTP requests:
Listening on http://0.0.0.0:7071/
Hit CTRL-C to exit...
Http Functions:
HttpTrigger-Java: http://localhost:7071/api/HttpTrigger-Java
Trigger the function from the command line using curl in a new terminal window:
curl -w '\n' -d LocalFunctionTest http://localhost:7071/api/HttpTrigger-Java
Hello, LocalFunctionTest
Use Ctrl-C
in the terminal to stop the function code.
The deploy process to Azure Functions uses account credentials from the Azure CLI. Log in with the Azure CLI before continuing.
az login
Deploy your code into a new Function app using the azure-functions:deploy
Maven target.
mvn azure-functions:deploy
When the deploy is complete, you see the URL you can use to access your Azure function app:
[INFO] Successfully deployed Function App with package.
[INFO] Deleting deployment package from Azure Storage...
[INFO] Successfully deleted deployment package fabrikam-function-20170920120101928.20170920143621915.zip
[INFO] Successfully deployed Function App at https://fabrikam-function-20170920120101928.azurewebsites.net
[INFO] ------------------------------------------------------------------------
Test the function app running on Azure using cURL
. You'll need to change the URL from the sample below to match the deployed URL for your own function app from the previous step.
curl -w '\n' -d AzureFunctionsTest https://fabrikam-functions-20170920120101928.azurewebsites.net/api/HttpTrigger-Java
Hello, AzureFunctionsTest
Edit the src/main.../Function.java
source file in the generated project to alter the text returned by your Function app. Change this line:
return request.createResponse(200, "Hello, " + name);
To the following:
return request.createResponse(200, "Hi, " + name);
Save the changes and redeploy by running azure-functions:deploy
from the terminal as before. The function app will be updated and this request:
curl -w '\n' -d AzureFunctionsTest https://fabrikam-functions-20170920120101928.azurewebsites.net/api/HttpTrigger-Java
Will have updated output:
Hi, AzureFunctionsTest
You've created a Java function app with a simple HTTP trigger and deployed it to Azure Functions.
- Review the Java Functions developer guide for more information on developing Java functions.
- Add additional functions with different triggers to your project using the
azure-functions:add
Maven target. - Write and debug functions locally with Visual Studio Code, IntelliJ, and Eclipse.
- Debug functions deployed in Azure with Visual Studio Code. See the Visual Studio Code serverless Java applications documentation for instructions.