Reference:
Develop your Spring Boot application with Visual Studio Code
-
Java
$ java -version java version "1.8.0_271" Java(TM) SE Runtime Environment (build 1.8.0_271-b09) Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)
-
Maven
choco install maven
-
Visual Studio Code
-
Language Support for Java(TM) by Red Hat
Need couple of minutes to install, please wait patiently.
-
Spring Initializr Java Support (to add dependencies)
Errors
-
Gradle error → Delete folder
C:\…\.gradle\wrapper\dists\gradle-7.5.1-bin
Could not install Gradle distribution from 'https://services.gradle.org/distributions/gradle-7.5.1-bin.zip' Timeout of 120000 reached waiting for exclusive access to file "C:\…\.gradle\wrapper\dists\gradle-7.5.1-bin"
-
Install failure for 3 & 4 → Download VSIX from Marketplace and install via VSIX
- Go to Spring Initializr
- Add dependencies
- Spring Web
- Spring Data JPA
- MySQL Driver
- Lombok
- Generate and Download
.zip
- Open with VS Code
- Use Run and Debug to Debug the application
- After making some changes, click restart to see the changes
Add @RestController
and API for "/"
@RestController
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping(value="/", method=RequestMethod.GET)
public String helloWorld() {
return "Hello World";
}
}
- JPA
- hibernate-validator (for JPA)
- Lombok: Reduce getter & setter boilerplate code with
@Getter @Setter
- Prerequisite: Have SQL installed.
- For example, with XAMPP.
# Log in to mysql
mysql -u root -p
# Creates the new database
mysql> create database springboot_db;
# Creates the user
mysql> create user 'springuser'@'%' identified by 'ThePassword';
# Gives all privileges to the new user on the newly created database
mysql> grant all on springboot_db.* to 'springuser'@'%';
Add [application.properties](http://application.properties)
or application.yaml
to src/main/resources
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/springboot_db
spring.datasource.username=springuser
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true