1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

spring tool suiteからAzureFunctionsのプロジェクトを作成する

Posted at

※Mavenの設定等々は公式や他記事に詳しい解説があるので割愛。それらの設定ができている前提で話を進めていきます。

プロジェクト作成

spring tool suiteを起動したら
左上の[ファイル]→[新規]→[Mavenプロジェクト]を選択

デフォルト・ワークスペースの設定(要するにプロジェクトフォルダの出力先)を設定したら次へ

アーキタイプの追加を選択し
アーキタイプ・グループ ID:com.microsoft.azure
アーキタイプ・アーティファクトID:azure-functions-archetype
アーキタイプ・バージョン:1.20(19/3時点の最新。参照:Maven Archetype For Azure Functions)
を選択
アーキタイプ.png
追加したアーキタイプを選択して次へ。

グループID等々を任意の値で入力。
(例)
groupId: hoge
artifactId: hoge.functions
version: 1.3.0
appName: hoge
appRegion: jp
resourceGroup: hoge.resource

バージョン参照:Maven Plugin For Azure Functionsのversion一覧

プロジェクトを実行してみる

Java11では現時点サポートされていない

現時点ではAzureFunctionsはJava8までしかサポートしていないためJava11で実行しようとしても実行できない。
参照:Azure Functions でサポートされている言語

Failed to execute goal com.microsoft.azure:azure-functions-maven-plugin:1.3.0:package (package-functions) on project hoge.functions: Local JDK version 11.0.2 does not meet the requirement of the Maven plugin for Azure Functions. The supported version is JDK 8

なので自分の環境ではzulu-8を使用して実行している。
[プロジェクト]を右クリック→[ビルド・パス]→[ビルド・パスの構成]でJREシステムライブラリーがJava8系になっているか確認。

なっていなければライブラリーの追加からJREシステムライブラリーを選択。
[インストール済みのJRE]から[追加]を選択しJDKまでのPathを設定する。

ローカルでAzureFunctionsの動作確認

※プロジェクトのアイコンに×マークがある場合は[プロジェクト]を右クリック→[Maven]→[プロジェクトの更新]

ビルド
[プロジェクト]を右クリック→[実行]からMaven Clean,Maven install,Maven ビルドを順に実行する。

AzureFunctionsの実行
[プロジェクト]を右クリック→[ローカル・ターミナルに表示]を選択、下にターミナルが表示されるので以下を実行。

mvn azure-functions:run

すると雷のようなマークが出力されるはず。
アーキタイプ4.png

AzureFunctionsにアクセスしてみる

出力された文字列に下記のURLが記述されているのでアクセスしてみる。

アーキタイプ4.png

まず初めにlocalhost:7071にアクセスするとこんな感じの画面になる。

アーキタイプ5.png

次にデフォルトで作成されているFunctionにアクセスしてみる。
デフォルトの関数の機能はnameとして受け取った文字にhelloを付け加えるだけの機能。

@FunctionName("HttpTrigger-Java")
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();
    }
}

別でコマンドプロンプトを起動し以下のコマンドを実行。

curl http://localhost:7071/api/HttpTrigger-Java?name=world
Hello,world

以下のコマンドでも可能。

curl -w '\n' -d world http://localhost:7071/api/HttpTrigger-Java

参考記事

javaでAzure functions

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?