Skip to content

luiscoco/Azure_Java_Function_Http_Triggered_using_IntelliJ

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 

Repository files navigation

How to create an Azure Java Function Http Triggered using IntelliJ

Install IntelliJ Community Edition: https://www.jetbrains.com/idea/download/?section=windows

image

Run IntelliJ Community Edition

image

Install "Azure Toolkit for IngelligJ" plugin

image

Create a new project

image

Select "Azure Functions", Function trigger "HttpTrigger", project SDK "Oracle openJDK version 20.0.1":

image

Select project manager tool "Maven", group name "com.example", artifact "azure-function-examples", version "1.0.0-SNAPSHOT", package name "org.example.functions":

image

Also set the project name "azure-function-examples" and project location "C:\azure-function-examples":

image

To avoid any problem with the Antivirus configure Microsoft Defender automatically:

image

This is the project structure:

image

This is the Azure Java Function source code:

package org.example.functions;

import java.util.*;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;

/**
 * Azure Functions with HTTP Trigger.
 */
public class HttpTriggerJava {
    /**
     * This function listens at endpoint "/api/HttpTriggerJava". Two ways to invoke it using "curl" command in bash:
     * 1. curl -d "HTTP Body" {your host}/api/HttpTriggerJava
     * 2. curl {your host}/api/HttpTriggerJava?name=HTTP%20Query
     */
    @FunctionName("HttpTriggerJava")
    public HttpResponseMessage run(
            @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) 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();
        }
    }
}

Run the application:

image

image

See the application output:

image

We input the Azure Function endpoint URL in the internet web browser:

http://localhost:54658/api/HttpTriggerJava

image

Also we can set the "name" parameter as a querystring in the above URL:

http://localhost:54658/api/HttpTriggerJava?name="my name is John"

image

Before deploying the Azure Java Function in the Azure Portal we can the deployment region "westeurope" in the pom.xml file:

image

We deploy the application clicking on the "Deploy to Azure..." link:

image

image

If you previously created a Function in Azure Portal then select it, otherwise we create a new Function in Azure portal to host our application

image

When the deployment is finished you can Login in Azure Portal and inside the Azure Functions list we select our new Function

image

image

We click on the button "Get Function Url" to copy the Azure Function URL:

image

image

image

We can also set the "name" parameter as a querystring in the Function URL endpoint:

https://app-azure-function-examples-231209092110.azurewebsites.net/api/HttpTriggerJava?code=3h0rA7jS-NQZyUcORLRVDWJ2MmTFuOPGKUd0rxHkArLTAzFuJ5x_QA==&name=%22my%20name%20is%20Peter%22

image

About

Azure Java Function Http Triggered using IntelliJ

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published