|
| 1 | +--- |
| 2 | +title: Agentic app with Semantic Kernel (Java) |
| 3 | +description: Learn how to quickly deploy a production-ready, agentic web application using Java with Azure App Service and Microsoft Semantic Kernel. |
| 4 | +ms.service: azure-app-service |
| 5 | +author: cephalin |
| 6 | +ms.author: cephalin |
| 7 | +ms.devlang: csharp |
| 8 | +ms.topic: tutorial |
| 9 | +ms.date: 07/16/2025 |
| 10 | +ms.custom: |
| 11 | + - devx-track-java |
| 12 | +ms.collection: ce-skilling-ai-copilot |
| 13 | +--- |
| 14 | + |
| 15 | +# Tutorial: Build an agentic web app in Azure App Service with Microsoft Semantic Kernel (Spring Boot) |
| 16 | + |
| 17 | +This tutorial demonstrates how to add agentic capability to an existing data-driven Spring Boot WebFlux CRUD application. It does this using Microsoft Semantic Kernel. |
| 18 | + |
| 19 | +If your web application already has useful features, like shopping, hotel booking, or data management, it's relatively straightforward to add agent functionality to your web application by wrapping those functionalities in a plugin (for Semantic Kernel). In this tutorial, you start with a simple to-do list app. By the end, you'll be able to create, update, and manage tasks with an agent in an App Service app. |
| 20 | + |
| 21 | +:::image type="content" source="media/tutorial-ai-agent-web-app-semantic-kernel-java/semantic-kernel-agent.png" alt-text="Screenshot of a chat completion session with a semantic kernel agent."::: |
| 22 | + |
| 23 | +> [!NOTE] |
| 24 | +> Azure AI Foundry Agent Service currently doesn't have a Java SDK, so isn't included in the scope of this article. |
| 25 | +
|
| 26 | +In this tutorial, you learn how to: |
| 27 | + |
| 28 | +> [!div class="checklist"] |
| 29 | +> * Convert existing app functionality into a plugin for Semantic Kernel. |
| 30 | +> * Add the plugin to a Semantic Kernel agent and use it in a web app. |
| 31 | +> * Assign the required permissions for managed identity connectivity. |
| 32 | +
|
| 33 | +## Prerequisites |
| 34 | + |
| 35 | +- An Azure account with an active subscription - [Create an account for free](https://azure.microsoft.com/free/java). |
| 36 | +- GitHub account to use GitHub Codespaces - [Learn more about GitHub Codespaces](https://docs.github.com/codespaces/overview). |
| 37 | + |
| 38 | +## Open the sample with Codespaces |
| 39 | + |
| 40 | +The easiest way to get started is by using GitHub Codespaces, which provides a complete development environment with all required tools preinstalled. |
| 41 | + |
| 42 | +[](https://codespaces.new/Azure-Samples/app-service-agentic-semantic-kernel-java) |
| 43 | + |
| 44 | +1. Navigate to the GitHub repository at [https://github.com/Azure-Samples/app-service-agentic-semantic-kernel-java](https://github.com/Azure-Samples/app-service-agentic-semantic-kernel-java). |
| 45 | + |
| 46 | +2. Select the **Code** button, select the **Codespaces** tab, and select **Create codespace on main**. |
| 47 | + |
| 48 | +3. Wait a few moments for your Codespace to initialize. When ready, you'll see a fully configured development environment in your browser. |
| 49 | + |
| 50 | +4. Run the application locally: |
| 51 | + |
| 52 | + ```bash |
| 53 | + mvn spring-boot:run |
| 54 | + ``` |
| 55 | + |
| 56 | +5. When you see **Your application running on port 8080 is available**, select **Open in Browser** and add a few tasks. |
| 57 | + |
| 58 | +## Review the agent code |
| 59 | + |
| 60 | +The Semantic Kernel agent is initialized in [src/main/java/com/example/crudtaskswithagent/controller/AgentController.java](https://github.com/Azure-Samples/app-service-agentic-semantic-kernel-java/blob/main/src/main/java/com/example/crudtaskswithagent/controller/AgentController.java), when the user enters the first prompt in a new browser session. |
| 61 | + |
| 62 | +You can find the initialization code in the `SemanticKernelAgentService` constructor (in [src/main/java/com/example/crudtaskswithagent/service/SemanticKernelAgentService.java](https://github.com/Azure-Samples/app-service-agentic-semantic-kernel-java/blob/main/src/main/java/com/example/crudtaskswithagent/service/SemanticKernelAgentService.java)). The initialization code does the following: |
| 63 | + |
| 64 | +- Creates a kernel with chat completion. |
| 65 | +- Adds a kernel plugin that encapsulates the functionality of the CRUD application (in *src/main/java/com/example/crudtaskswithagent/plugin/TaskCrudPlugin.java*). The interesting parts of the plugin are the `DefineKernelFunction` annotations on the method declarations and the `description` and `returnType` parameters that help the kernel call the plugin intelligently. |
| 66 | +- Creates a [chat completion agent](/semantic-kernel/frameworks/agent/agent-types/chat-completion-agent?pivots=programming-language-java), and configures it to let the AI model automatically invoke functions (`FunctionChoiceBehavior.auto(true)`). |
| 67 | +- Creates an agent thread that automatically manages the chat history. |
| 68 | + |
| 69 | +:::code language="csharp" source="~/app-service-agentic-semantic-kernel-java/src/main/java/com/example/crudtaskswithagent/service/SemanticKernelAgentService.java" range="38-90" highlight="11-48,58" ::: |
| 70 | + |
| 71 | +Each time the prompt is received, the server code uses `ChatCompletionAgent.invokeAsync()` to invoke the agent with the user prompt and the agent thread. The agent thread keeps track of the chat history. |
| 72 | + |
| 73 | +:::code language="csharp" source="~/app-service-agentic-semantic-kernel-java/src/main/java/com/example/crudtaskswithagent/service/SemanticKernelAgentService.java" range="109-158" highlight="8" ::: |
| 74 | + |
| 75 | +## Deploy the sample application |
| 76 | + |
| 77 | +The sample respository contains an Azure Developer CLI (AZD) template, which creates an App Service app with managed identity and deploys your sample application. |
| 78 | + |
| 79 | +1. In the terminal, log into Azure using Azure Developer CLI: |
| 80 | + |
| 81 | + ```bash |
| 82 | + azd auth login |
| 83 | + ``` |
| 84 | + |
| 85 | + Follow the instructions to complete the authentication process. |
| 86 | + |
| 87 | +4. Deploy the Azure App Service app with the AZD template: |
| 88 | + |
| 89 | + ```bash |
| 90 | + azd up |
| 91 | + ``` |
| 92 | + |
| 93 | +1. When prompted, give the following answers: |
| 94 | + |
| 95 | + |Question |Answer | |
| 96 | + |---------|---------| |
| 97 | + |Enter a new environment name: | Type a unique name. | |
| 98 | + |Select an Azure Subscription to use: | Select the subscription. | |
| 99 | + |Pick a resource group to use: | Select **Create a new resource group**. | |
| 100 | + |Select a location to create the resource group in:| Select any region. The resources will actually be created in **East US 2**.| |
| 101 | + |Enter a name for the new resource group:| Type **Enter**.| |
| 102 | + |
| 103 | +1. In the AZD output, find the URL of your app and navigate to it in the browser. The URL looks like this in the AZD output: |
| 104 | + |
| 105 | + <pre> |
| 106 | + Deploying services (azd deploy) |
| 107 | + |
| 108 | + (✓) Done: Deploying service web |
| 109 | + - Endpoint: <URL> |
| 110 | + </pre> |
| 111 | + |
| 112 | +1. After successful deployment, you'll see a URL for your deployed application. |
| 113 | + |
| 114 | + You now have an App Service app with a system-assigned managed identity. |
| 115 | + |
| 116 | +## Create and configure the Azure AI Foundry resource |
| 117 | + |
| 118 | +1. In the [Azure AI Foundry portal](https://ai.azure.com), deploy a model of your choice (see [Quickstart: Get started with Azure AI Foundry](/azure/ai-foundry/quickstarts/get-started-code?tabs=azure-ai-foundry&pivots=fdp-project)). A project and a model deployment are created for you in the process. |
| 119 | + |
| 120 | +1. From the left menu, select **Overview**. |
| 121 | + |
| 122 | +1. Select **Azure OpenAI** and copy the URL in **Azure OpenAI endpoint** for later. |
| 123 | + |
| 124 | + :::image type="content" source="media/tutorial-ai-agent-web-app-semantic-kernel-java/foundry-openai-endpoint.png" alt-text="Screenshot showing how to copy the OpenAI endpoint in the foundry portal."::: |
| 125 | + |
| 126 | +1. Select **Models + endpoints** and copy the name of the model deployment for later. |
| 127 | + |
| 128 | + :::image type="content" source="media/tutorial-ai-agent-web-app-semantic-kernel-java/foundry-model-deployment.png" alt-text="Screenshot showing how to copy the model deployment name in the foundry portal."::: |
| 129 | + |
| 130 | +## Assign required permissions |
| 131 | + |
| 132 | +1. At the upper right corner of the foundry portal, select the name of the resource, then select **Resource Group** to open it in the Azure portal. |
| 133 | + |
| 134 | + :::image type="content" source="media/tutorial-ai-agent-web-app-semantic-kernel-foundry-dotnet/go-to-azure-portal.png" alt-text="Screenshot showing how to quickly go to the resource group view for the foundry resource in the Azure portal."::: |
| 135 | + |
| 136 | +1. Add a role for each of the two resources for the App Service app's manage identity using the following table: |
| 137 | + |
| 138 | + | Target resource | Required role | Needed for | |
| 139 | + |--------------------------------|-------------------------------------|-------------------------| |
| 140 | + | Azure AI Foundry | Cognitive Services OpenAI User | The chat completion service in the semantic kernel. | |
| 141 | + |
| 142 | + For instructions, see [Assign Azure roles using the Azure portal](/azure/role-based-access-control/role-assignments-portal). |
| 143 | + |
| 144 | +## Configure connection variables in your sample application |
| 145 | + |
| 146 | +1. Open *src/main/resources/application.properties*. Using the values you copied earlier from the AI Foundry portal, configure the following variables: |
| 147 | + |
| 148 | + | Variable | Description | |
| 149 | + |-------------------------------|----------------------------------------------------------| |
| 150 | + | `azure.openai.endpoint` | Azure OpenAI endpoint (copied from the Overview page). This is needed by the Semantic Kernel agent. | |
| 151 | + | `azure.openai.deployment` | Model name in the deployment (copied from the Models + endpoints page). This is needed by the Semantic Kernel agent. | |
| 152 | + |
| 153 | + > [!NOTE] |
| 154 | + > To keep the tutorial simple, you'll use these variables in *src/main/resources/application.properties* instead of overwriting them with app settings in App Service. |
| 155 | +
|
| 156 | +1. Sign in to Azure with the Azure CLI: |
| 157 | + |
| 158 | + ```bash |
| 159 | + az login |
| 160 | + ``` |
| 161 | + |
| 162 | + This allows the Azure Identity client library in the sample code to receive an authentication token for the logged in user. Remember that you added the required role for this user earlier. |
| 163 | + |
| 164 | +1. Run the application locally: |
| 165 | + |
| 166 | + ```bash |
| 167 | + mvn spring-boot:run |
| 168 | + ``` |
| 169 | + |
| 170 | +1. When you see **Your application running on port 8080 is available**, select **Open in Browser**. |
| 171 | + |
| 172 | +1. Try out the chat interface. If you get a response, your application is connecting successfully to the Azure AI Foundry resource. |
| 173 | + |
| 174 | +1. Back in the GitHub codespace, deploy your app changes. |
| 175 | + |
| 176 | + ```bash |
| 177 | + azd up |
| 178 | + ``` |
| 179 | + |
| 180 | +1. Navigate to the deployed application again and test the chat agents. |
| 181 | + |
| 182 | +:::image type="content" source="media/tutorial-ai-agent-web-app-semantic-kernel-java/semantic-kernel-agent.png" alt-text="Screenshot of a chat completion session with a semantic kernel agent."::: |
| 183 | + |
| 184 | +## Clean up resources |
| 185 | + |
| 186 | +When you're done with the application, you can delete the App Service resources to avoid incurring further costs: |
| 187 | +
|
| 188 | +```bash |
| 189 | +azd down --purge |
| 190 | +``` |
| 191 | +
|
| 192 | +Since the AZD template doesn't include the Azure AI Foundry resources, you need to delete them manually if you want. |
| 193 | + |
| 194 | +## More resources |
| 195 | + |
| 196 | +- [Integrate AI into your Azure App Service applications](overview-ai-integration.md) |
| 197 | +- [Introduction to Semantic Kernel](/semantic-kernel/overview/) |
0 commit comments