dcsimg
April 21, 2019
Hot Topics:

Developing a Spring Boot Application Using STS

Spring Boot is a framework and tool suite for developing and deploying Spring-based applications quickly and with very little configuration. It even comes with an embedded Tomcat Web server for running your applications as a standalone app!

Although Spring Boot is not associated with any particular IDE, being a Java tool set makes it a perfect candidate for development in Eclipse. As it happens, Spring offers the Spring Tool Suite (STS) for your favorite coding environment. Now at version 4, Spring Tools provides top-notch support for developing Spring-based enterprise applications in Eclipse, Visual Studio Code, and Atom IDE. In this tutorial, we'll employ STS to create a basic Hello World Web app in Eclipse.

Installing STS

The easiest way to install STS is right from within Eclipse. Just select Help > Eclipse Marketplace... from the main menu and type "STS" in the search bar of the Eclipse Marketplace dialog. Once it appears in the search results, click the "install" button to proceed, as shown in Figure 1:

STS in Marketplace
Figure 1: STS in Marketplace

Creating a New Project

STS includes its own starter project to get you started. It is accessible from the New Project Wizard. Navigate to File > New > Other... from the main menu or press Ctrl+N on your keyboard to open the wizard. In the search field, type "spring". You'll see the "Spring Starter Project" in the "Spring Boot" folder, as you can see in Figure 2. Select it and confirm your choice by clicking on the "Next" button:

STS Starter Package
Figure 2: STS Starter Package

On the New Spring Starter Project dialog, you'll have to choose a name for your project. Let's call it "HelloWorld". Select Maven as the build tool, and JAR packaging and your Java version. Assign a Group of "com.developer" for the package name. Artifact is the name of the JAR file you are going to build. For example, if you use "helloworld" as the artifact, the final JAR file will be called helloworld.jar. When you're done, your project settings should match these shown in Figure 3:

Project Settings
Figure 3: Project Settings

Click Next.

In the New Spring Starter Project Dependencies window, use the search field and type "web" into it. Then, select the Web checkbox (see Figure 4).

Project Dependencies
Figure 4: Project Dependencies

You can click the Finish button now. STS will create the project for you and download all of the required dependencies.

The project will contain one main Java class, called HelloWorldApplication.java, as well as a Java class for testing purposes, an empty properties file, a Maven POM file, and two files to execute the application from the command line. Figure 5 shows the project structure and the content of the main class in the editor:

Project Structure and Main Class
Figure 5: Project Structure and Main Class

Setting the Request Mapping

Our app will know what content to show when we navigate to the root URL via a request mapping. Modify the SpringBootExampleApplication.java file as shown in the following code:

package com.developer.helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure
   .SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@SpringBootApplication
public class HelloWorldApplication {

   @RequestMapping("/")
   @ResponseBody
   String home() {
      return "Hello World!";
   }

   public static void main(String[] args) {
      SpringApplication.run(HelloWorldApplication.class, args);
   }

}

Running Your Application

In Eclipse, there's no need to run the application from the command line. Just right-click your project and select Run As > Spring Boot App from the context menu, as demonstrated in Figure 6:

Starting the app
Figure 6: Starting the app

That will bootstrap the embedded Tomcat server, deploy your app, and map the URLs.

Once the application is started, launch your browser and navigate to http://localhost:8080 to see our message, as shown in Figure 7:

The app, running in a browser
Figure 7: The app, running in a browser

Conclusion

Thanks to STS tooling, there's no reason to build your Spring Boot apps from the command line. You can do all of your work within your favorite IDE.






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.


Thanks for your registration, follow us on our social networks to keep up-to-date