is now available! Read about the new features and fixes from November.

Java Web Apps with Visual Studio Code

This tutorial shows you how to create a Java web application with Visual Studio Code. You'll learn how to run, debug, and edit the Java web app locally and eventually on the cloud.

Scenario

A simple Spring Boot Getting Started web app

Greeting from Java

Before you begin

Before running and deploying this sample, you must have the Java SE Development Kit (JDK) and Apache Maven build tools on your local development environment. If you don't have, please install them.

Download and install the Extension Pack for Java, which has JDK 11 included.

Note: The JAVA_HOME environment variable must be set to the install location of the JDK to complete this tutorial.

Download Apache Maven version 3 or greater:

Download Apache Maven

Install Apache Maven for your local development environment:

Install Apache Maven

Download and test the Spring Boot app

Clone the Spring Boot Getting Started sample project to your local machine. You can clone a Git repository with the Git: Clone command in the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). Paste https://github.com/spring-guides/gs-spring-boot.git as the URL of the remote repository and then decide the parent directory under which to put the local repository. After that, open the complete folder within the cloned repository in VS Code by navigating to the folder and typing code ..

Note: You can install Visual Studio Code from https://code.visualstudio.com and Git from https://git-scm.com.

Clone Spring Repository

From within VS Code, open any of the Java files within the complete folder (for example src\main\java\hello\Application.java). If you don't have the Java language extensions installed for VS Code, you will be prompted to install the Microsoft Extension Pack for Java. Follow the instructions and reload VS Code after the installation.

Install Java Extensions

Once you have the Extension Pack for Java installed, it will automatically build the project for you (this may take several minutes). You can run the application within VS Code by pressing F5 and selecting the Java environment. The Java Debug extension will generate a debugging configuration file launch.json for you under a .vscode folder in your project. You can see build progress in the VS Code Status Bar and when everything is finished, the final active debug configuration is displayed.

debug configuration in the Status Bar

You can learn more about how VS Code launches your application in Debugging Launch Configurations. Press F5 again to launch the debugger.

Run Spring Boot

Test the web app by browsing to http://localhost:8080 using a web browser. You should see the following message displayed: "Greetings from Spring Boot!".

Greeting from Spring

Make a change

Let's now edit HelloController.java to change "Greetings from Spring Boot!" to something else like "Hello World". VS Code provides a great editing experience for Java, check out Navigating and edit Java to learn about VS Code's editing and code navigation features.

Click the Restart button on the top of the editor to relaunch the app and see result by reloading the browser.

Restart Application

Debug the application

Set a breakpoint (F9) in the application source code, and reload your browser to hit the breakpoint.

Debug Application

If you would like to learn more about debugging Java with VS Code, you can read Java Debugging.

Congratulations, you have your first Spring Boot web app running locally! Read on to learn how to host it in the cloud.

Deploy Web Apps to the cloud

We just built a Java web application and ran it locally. Now you will learn how to deploy from Visual Studio Code and run it on Azure in the cloud.

If you don't have an Azure subscription, you can sign up for a free Azure account.

Create your free Azure account

Install the Azure App Service extension

The Azure App Service extension is used to create, manage, and deploy to Azure App Service with key features including:

  • Create new Azure Web App/Deployment Slot
  • Deploy to Azure Web App/Deployment Slot
  • Start, stop, and restart the Azure Web App/Deployment Slot
  • View a Web App's log files
  • Swap Deployment Slots

To install the Azure App Service extension, open the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)) and search for azure app service to filter the results. Select the Microsoft Azure App Service extension. For a more command-line Maven-centric experience, you can also check out the Maven plugin for Azure App Service Linux tutorial.

Sign in to your Azure subscription

To sign in to Azure, run Azure: Sign In from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). Or you can sign in to your Azure Account by clicking Sign in to Azure... in RESOURCES Explorer.

Azure sign in code

Create a new Web App on Azure

Once the extension is installed, you can take the following steps to create a new Web App on Azure.

  1. Click Create button on the RESOURCES Explorer view and select Create App Service Web App....

  2. Enter a unique name for the new Web App.

  3. Select the runtime task of the Web App, for example Java 17.

  4. Select the Java web server stack, for example Java SE.

  5. Select a pricing tier.

Create a Web App

Build and deploy to a Web App

The deploy process leverages the Azure Resources extension (installed along with the Azure App Service extension as a dependency) and you need to sign in with your Azure subscription. If you do not have an Azure subscription, sign up today for a free 30 day account and get $200 in Azure Credits to try out any combination of Azure services.

Once you have signed in, you can open the command prompt or terminal window and build the project using Maven commands. This will generate a new war or jar artifact in the target directory.

mvn clean package

After building the project, open the target directory in VS Code Explorer. Right-click on the artifact and choose Deploy to Web App, and follow the prompts to choose the Web App for your deployment.

Deploy to Web App

Open the Output window in VS Code to view the deployment logs. Once the deployment is completed, it will print out the URL for your Web App. Click the link to open it in a browser, you can see the web app running on Azure!

Greeting from Spring Boot

Note: For more advanced features of App Service, you can check out the Azure App Service extension.

Connect with data services

Azure Cosmos DB is a globally distributed database service that allows developers to work with data using a variety of standard APIs, such as SQL, MongoDB, Cassandra, Graph, and Table.

The Spring Boot Starter makes it easy to store data in and retrieve data from your Azure Cosmos DB for NoSQL database.

Create an Azure Cosmos DB entity on Azure

  1. Go to Azure portal and click the '+' to Create a resource.
  2. Click Databases, and then click Azure Cosmos DB to create your database.
  3. Select SQL (Document DB) API and type in other information for your database.
  4. Navigate to the database you have created, click Keys, and copy your URI and PRIMARY KEY for your database.

Config your project

  1. You can start from the Spring Data Azure Cosmos DB Sample Project.

  2. Navigate to src/main/resources and open application.properties. Replace below properties in application.properties with information of your database.

    azure.documentdb.uri=your-documentdb-uri
    azure.documentdb.key=your-documentdb-key
    azure.documentdb.database=your-documentdb-databasename
    

Run and debug the application

You can press F5 to run your application. To check the result, open the Azure portal and access your Azure Cosmos DB instance. Select Data Explorer, and next choose Documents. Data is shown if it's successfully written into Azure Cosmos DB. You can also browse your data entries in Azure Cosmos DB with the Azure Databases extension.

After setting a breakpoint (F9) in your source code, refresh your browser to hit the breakpoint. Details about debugging can be found in Java Debugging

Alternatively, you can also use Maven to package and run your project as steps below:

  1. Navigate to the directory azure-spring-boot and run the command.

    mvn install
    
  2. Navigate to the directory azure-documentdb-spring-boot-sample and run the command.

    mvn package
    java -jar target/azure-documentdb-spring-boot-sample-0.0.1-SNAPSHOT.jar
    

Next steps