Spring Boot Application for Beginners

June 14, 2023

The intended audience for this article is Java developers who are looking to use Spring Boot as a framework. 
Upon completing this article, you will understand...

  1. How to implement a small-scale RESTful API provider.
  2. How to use Maven to package the correct WAR file for different environments.

Let's get started.


First, create a new Maven project. In the pom.xml file, include the following dependencies: spring-boot-starter-web, spring-boot-starter-test, and junit.

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>
Next, we are going to write a simple controller. First, annotate your class with @RestController and @RequestMapping("/api"). Then, within this class, create a method and annotate it with @GetMapping("/alive"). Here's an example of what your code might look like:

@RestController
@RequestMapping("/api")
public class BotEntryPointController {

  @GetMapping("/alive")
  public String alive() {
      return "alive";
  }
}

 let's write a main method to act as the entry point of our application. This is where we'll launch the application. To do this, you'll need to annotate the class with @SpringBootApplication, and then call SpringApplication.run(YourApp.class, args);. Here's an example:
@SpringBootApplication
public class NiceBotApplication {
  public static void main(String[] args) {

      SpringApplication.run(NiceBotApplication.class, args);
  }
}


Now, let's write a test. First, annotate your class with @RunWith(SpringRunner.class) and @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT). Then, within this class, create a test method. Here's an example of what your test might look like:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public class BotEntryPointControllerTest {

  @Autowired
  private TestRestTemplate restTemplate;

  @Test
  public void aliveTest() {

      ResponseEntity<String> response = restTemplate.getForEntity("/api/alive", String.class);
      assertEquals(HttpStatus.OK, response.getStatusCode());
      assertEquals("alive", response.getBody());

  }
}

In this example, we wrote a simple test that checks if the '/api/alive' endpoint is responding correctly.
You can quickly run this test in Eclipse by using the shortcut Alt + Shift + X, T."

To package a Maven project, use the following command:
mvn clean package -DskipTests -Pprod

The 'install' command will install the artifact into the local repository for other projects to use. The 'package' command, on the other hand, will generate files needed for deployment and distribution.

The '-D' option is used to specify properties. For example:
mvn clean install -DskipTests=true -Dapp.env=prod

In your pom.xml, you can access this property using ${app.env}:

<build>
  <plugins>
    <plugin>
      <groupId>com.example</groupId>
      <artifactId>example-plugin</artifactId>
      <version>1.0.0</version>
      <configuration>
        <environment>${app.env}</environment>
      </configuration>
    </plugin>
  </plugins>
</build>

The '-P' option is used to specify profiles. For example:
mvn clean install -Pprod

And here is how you can define profiles in your pom.xml:
<profiles>
  <profile>
    <id>dev</id>
    <properties>
      <app.env>dev</app.env>
    </properties>
  </profile>
  <profile>
    <id>prod</id>
    <properties>
      <app.env>prod</app.env>
    </properties>
  </profile>
</profiles>

Now you have a working base application ready to be expanded.