Prerequisite
Install homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install java
brew install java
# if you want to install multi-version
brew install --cask adoptopenjdk8
brew install --cask adoptopenjdk11
brew install --cask adoptopenjdk16
these will be installed into /Library/Java/JavaVirtualMachines/ which is the traditional location on Mac OSX.
For M1 Mac, Since zsh is the default Terminal shell, so place script below either in your ~/zprofile / or ~/.zshenv or ~/.zshrc
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'# default to Java 11
java11
then you can use command java8 or java11 to switch your java runtime version
$ java8
$ java11# or you can echo to show java_home path
$ echo $JAVA_HOME
Install maven
brew install maven
Check maven version
mvn --version
Install spring boot
brew tap spring-io/tap
brew install spring-boot
In VSCode install extension
#Java Extension Pack
https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack#Maven for java
https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-maven#Spring Boot Extension Pack
https://marketplace.visualstudio.com/items?itemName=Pivotal.vscode-boot-dev-pack
Setup Setting.json in vscode
Code > Preference > Settings
Search “maven” and click Edit in setting.json
Initialise Spring Boot project
In vscode, command + shift + p and choose Spring initializr: Create a Maven Project
Name your project, that says, Demo, then I generate DemoApplication
Add a test restful API in DemoApplication.java (Later should better add into another file call xxxController rather than in Application level
Import 2 packages below
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Modify the class
@RestController
@SpringBootApplication
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} @RequestMapping("/")
public String home() {
return "Hello world, Java Spring boot.";
}
}
To run project, you can either use command or debug mode to run it,
Start up project by using command (base on official link) https://spring.io/guides/gs/rest-service/
./mvnw spring-boot:run
Start up project to use vscode debug mode, first add .vscode/launch.json if you want to enable debug mode
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Launch Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "Launch DemoApplication",
"request": "launch",
"mainClass": "com.example.demo.DemoApplication",
"projectName": "demo"
}
]
}
Click F5