Spring Visual Studio Code



You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. To refresh your session. Visual Studio Code also supports more complex Java projects, see Project Management. Editing source code. You can use code snippets to scaffold your classes and methods. VS Code also provides IntelliSense for code completion, and various refactor methods. To learn more about editing Java, see Java Editing. Running and debugging your program.

This extension for Visual Studio Code adds useful snippets for Spring Boot. These are especially useful for creating REST apis, and configuring database connection properties.

Usage

For a detailed walkthrough, please use this youtube video - https://www.youtube.com/watch?v=qI7hTw8aMaU

Type part of a snippet, press enter, and the snippet unfolds.

Alternatively, press Ctrl+Space (Windows, Linux) or Cmd+Space (macOS) to activate snippets from within the editor.

Java Snippets

SnippetPurpose
spring-crudctrlSpring controller with CRUD actions implemented
spring-crudrepoSpring Data JPA CRUD repository with REST api annotation
spring-jparepoSpring Data JPA repository with REST api annotation
spring-entityJPA entity with ID field
spring-lombok-entityJPA entity with ID field and Lombok @Data annotation
spring-ctrl-getallController GET action for collection
spring-ctrl-getoneController GET action for single item
spring-ctrl-postController POST action
spring-ctrl-putController PUT action
spring-ctrl-deleteController DELETE action

application.properties / application.yaml Snippets

SnippetPurpose
spring-sqliteSQLite database jdbc url and properties
spring-sqlserverSQL Server database jdbc url and properties
spring-oracleOracle database jdbc url and properties
spring-postgresqlPostgreSQL database jdbc url and properties
spring-mysqlMySQL database jdbc url and properties
spring-h2H2 database jdbc url and properties

Installation

  1. Install Visual Studio Code 1.44.0 or higher
  2. Launch Code
  3. From the command palette Ctrl-Shift-P (Windows, Linux) or Cmd-Shift-P (OSX)
  4. Select Install Extension
  5. Choose the extension
  6. Reload Visual Studio Code

Overview

In this tutorial, we are going to learn how to how to build a Spring Boot REST CRUD application with PostgreSQL database as backend and Visual Studio Code as editor.

Spring visual studio code

Technologies Used

  • Java 1.8
  • Spring Boot 2.2.6
  • PostgreSQL Database
  • Visual Studio Code
  • Postman (to test API)

Project Structure

  • EmployeeController contains mapping methods to handle RESTful requests.
  • IEmployeeDAO deals with database operations.
  • EmployeeDAO is the implementation of IEmployeeDAO.
  • Employee is the Data Model class.
  • application.properties is the configuration file where we can add application configurations such as database configuration.
  • pom.xml contains dependencies of the project.
Studio

Database Setup

We are using PostgreSQL Database as backend for this project, first we need to install PostgreSQL and setup Database. Tuxera ntfs full mojaveyellowray.

Download the latest version of PostgreSQL from here. After the installation run pgAdmin IDE to create and setup the database.

Create a database with name employee

Under Tools -> Query Tool, run the below script to create the table emp_master.

Spring Boot With Visual Studio Code

Create Spring Boot Project

In this project, we are using Visual Code to create the project. Visual Code is an Open Source and lightweight development environment developed by Microsoft. We can download it from here.

Refer Spring Boot in Visual Studio Code to create and run the project under Visual Studio Code.

POM Dependencies

We need to add the following project dependencies to pom.xml file.

Application Properties

Add database configurations to srcmainresourcesapplication.properties file.

Data Model

Create a model class forEmployee,

Spring Visual Studio Code

Data Access Objects

Create an Interface IEmployeeDAO with basic database CRUD functions, Aio club.

Create implementation class EmployeeDAO for IEmployeeDAO,

Controllers

Finally we will create controllers to handle RESTful API requests,

Visual Studio Code Spring Boot App

Test with Postman

Visual Studio Or Visual Code

Conclusion

In this tutorial, we have learned how to create a Spring Boot REST CRUD API application using Visual Studio Code and PostgreSQL.

Visual Studio Code Spring Boot

The full source code of the sample used for this tutorial can be find on Github