Skip to content

Latest commit

 

History

History
164 lines (109 loc) · 4.15 KB

File metadata and controls

164 lines (109 loc) · 4.15 KB

<< Back To Previous Page

Lab 1.1: Up and Going with Spring Boot

Introduction

Spring Boot is a framework for Java that accelerates web development.

Step 1: Open Visual Studio Code and create base project.

Press Ctrl + Shift + P to open command selector.

  • Type Spring Initializr.

image

  • Select Spring Initializr: Create a Maven Project...

  • Select 3.3.5 alt text

  • Select Java image

  • Set the group Id of your preference and press Enter image

  • Set the Artifact Id of your preference and press Enter image

  • Select Jar as the package type image

  • Select Java version 17 image

Step 2: Examine the code of the basic application

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.

Step 2: Do a 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] ------------------------------------------------------------------------

Step 3: Run my App

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.

Step 4: Test my app

Let's test it! Open your browser to http://localhost:8080

You should get the following response:

Hello from Spring Boot!