Skip to content

SpringBoot Lab

The goal here is to create a sample micro service coded in Java Spring Boot built with Maven.

  1. Create the micro service using Spring Initializr online tool.

    Warning

    Don't forget to add "Spring Web" dependency !

    Spring Initializr Settings

    • Click on "Generate" to download the zipped sources.

    • Extract them in your work directory.

    • Import it in your favorite Java IDE as a Maven project.

      Tip

      IntelliJ Idea Community Edition is a very good choice ... Just saying !

      Tip

      If you don't use IntelliJ and need to install Maven, here is the command 😉

      1
      choco install maven
      

  2. Edit the code

    • Modify the main class :

      src/main/java/com/yncrea/cloudcomputing/CloudcomputingApplication.java

      as below :

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      package com.yncrea.cloudcomputing;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.ResponseBody;
      import org.springframework.web.bind.annotation.RestController;
      
      @RestController
      @SpringBootApplication
      public class CloudcomputingApplication {
      
          @RequestMapping("/")
          public String home() {
              String hostname = System.getenv("HOSTNAME");
              return "Hello Docker World - hostname : " + hostname;
          }
      
          @RequestMapping("/hello")
          @ResponseBody
          public String sayHello() {
              return "Hello " + System.getenv("GREETING");
          }
      
          @RequestMapping("/secret")
          @ResponseBody
          public String getSecret() {
              String secret = System.getenv("MY_SECRET") == null ? "" : System.getenv("MY_SECRET");
              return "Secret : " + secret;
          }
      
          public static void main(String[] args) {
              SpringApplication.run(CloudcomputingApplication.class, args);
          }
      
      }
      
  3. Test it with the embedded Tomcat Server

    • Build the project using your IDE Maven Window

      or

      Using the command line :

      1
      mvn clean install
      

    • Run the application using your IDE Run Configuration

      or

      Using the command line :

      1
      mvn spring-boot:run
      

    • The application is now accessible at http://localhost:8080

      Tip

      Try visiting http://localhost:8080/hello to reach your 2nd API 😏