Azure Tips and Tricks Part 61 - Java in Azure Function with VS Code

2 minute read

Intro

Most folks aren’t aware of how powerful the Azure platform really is. As I’ve been presenting topics on Azure, I’ve had many people say, “How did you do that?” So I’ll be documenting my tips and tricks for Azure in these posts.

The Complete List of Azure Tips and Tricks

Available Now!

Java in Azure Function with VS Code

I just spent time over the weekend playing with Java in Azure Functions. As a prior Java developer and Azure Functions fan, you’ll be happy to know that you can scaffold an app in a couple of ways.

Getting setup

You’ll need a few things before getting started such as:

.NET Core,JDK, Azure CLI, Apache Maven and Node.js.

Once that is ready, you can easily turn this on by npm install -g azure-functions-core-tools@core. You should see the following:


> azure-functions-core-tools@2.0.1-beta.18 uninstall /usr/local/lib/node_modules/azure-functions-core-tools
> node lib/uninstall.js

deleting /Users/mbcrump/.azurefunctions/bin
/usr/local/bin/func -> /usr/local/lib/node_modules/azure-functions-core-tools/lib/main.js
/usr/local/bin/azfun -> /usr/local/lib/node_modules/azure-functions-core-tools/lib/main.js
/usr/local/bin/azurefunctions -> /usr/local/lib/node_modules/azure-functions-core-tools/lib/main.js

> azure-functions-core-tools@2.0.1-beta.22 postinstall /usr/local/lib/node_modules/azure-functions-core-tools
> node lib/install.js

[==================] Downloading Azure Functions Cli
+ azure-functions-core-tools@2.0.1-beta.22
added 1 package and updated 6 packages in 18.873s

Use Maven to generate a Java Azure Functions app

You’ll now take advantage of Maven and will scaffold a new Azure Functions project with mvn archetype:generate -DarchetypeGroupId=com.microsoft.azure -DarchetypeArtifactId=azure-functions-archetype.

Remember this! There is a ridiculous number of things you can do with Maven. Check out the GitHub repo for usage and configuration examples.

I’d also recommend installing the extension that is suggested for Java projects:

OK, cool! We have scaffolded a Java Azure Function project and we are using VS Code to edit it.

Make note of the following comments in the Java code as we’ll use the 2nd curl command shortly:

    /**
     * This function listens at endpoint "/api/hello". Two ways to invoke it using "curl" command in bash:
     * 1. curl -d "HTTP Body" {your host}/api/hello
     * 2. curl {your host}/api/hello?name=HTTP%20Query
     */

You can now run mvn clean package and mvn azure-functions:run from inside the integrated terminal or command prompt. You’ll now have a localhost URL that you run the curl command curl {your host}/api/hello?name=HTTP%20Query to ensure it was working properly. You should see Hello HTTP Query!.

Use the plugin for Visual Studio Code

Remember this! Keep in mind that you’ll need all the prerequisites covered earlier in this post before proceeding.

I covered the plugin back in post 50, but basically grab the plugin and login into your Azure account and create a new Azure Function as shown below:

Click New and you’ll see the following:

Now you’ll walk through the same steps that you did before through the command line, but now right inside VS Code.

Want more Azure Tips and Tricks?

If you’d like to learn more Azure Tips and Tricks, then follow me on twitter or stay tuned to this blog! I’d also love to hear your tips and tricks for working in Azure, just leave a comment below.

Leave a Comment