Spring Boot is a framework for Java that accelerates web development.
Press Ctrl + Shift + P to open command selector.
- Type Spring Initializr.
-
Select Spring Initializr: Create a Maven Project...
Look at the basic application
package com.elephantscale.course;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CourseApplication {
public static void main(String[] args) {
SpringApplication.run(CourseApplication.class, args);
}
}
// HelloController.java
package com.elephantscale.course;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
What does this code do? Not too much. It maps to the "/" endpoint, which is what happens if you just have the basic URL. (In this case http://localhost:8080).
Before we can go there, we have to build.
Go to the command line, and type
cd api-lab-1
./mvnw clean package # Linux / Mac
Windows users should do the following:
cd api-lab-1
mvnw.cmd clean package
You will see a LOT of output. If you have not run maven before, get ready for a huge session of downloading dependencies.
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.elephantscale:api-lab-1 >---------------------
[INFO] Building api-lab-1 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
And so on.
If things went well, you should see something like this:
[INFO] --- spring-boot-maven-plugin:3.1.x:repackage (default) @ api-lab-1 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.896 s
[INFO] Finished at: 2024-10-30T22:58:35-07:00
[INFO] ------------------------------------------------------------------------
Let us now run our application. We can do that with Maven was well.
./mvn spring-boot:run
After a bunch of maven output, we will get the following:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.2.RELEASE)
Followed by a bunch of more messages. The ones we are most interested are the following:
2018-06-12 23:10:35.468 INFO 51883 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
Which indicates that Tomcat has been started on port 8080.
Let's test it! Open your browser to http://localhost:8080
You should get the following response:
Hello from Spring Boot!