• Home  / 
  • Spring Boot
  •  /  How to create a simple web application with Spring Boot

How to create a simple web application with Spring Boot

By Daniel Vladimirov / last month

In this quick tutorial we are going to start up a Spring Boot application and configure a single and very simple controller that will respond to a GET request and greet the user with a message. There is very little time involved in initially setting up Spring Boot for development, which is one of the reasons it’s so popular for quickly spinning up Java applications.

First, you need to create a project in your favorite IDE and set it up with Maven. Then, we’re going to look for Spring Boot in the Central Maven repository. You can of course use Google as well, to quickly find the needed dependency. What we’re looking for is – spring-boot-starter-web, which contains everything we need for a fresh web application.

This is the resulting POM file:

The compiler settings are not really relevant for this tutorial, it should work without them as well, I just set them up whenever I create a new project, because sometimes IDEs get confused.

After setting up the POM file, you need to wait for the dependencies to get resolved from the maven repository.

Next, create a package under the main-java folder structure, the package name doesn’t matter, it’s just a good practice not to use the default package.

Create a class named Application (or anything you want) and annotate it with @SpringBootApplication. This annotation is an alias for many other annotations, needed to start up and configure your new Spring Boot application and was created as a quality of life improvement, since annotations in Spring generally can be overwhelming at times. What the annotation does – it simply enables Spring to configure itself with sensible default values and also sets the package, of the annotated class, to be processed for other Spring components, like controller classes for example, which we are going to create next.

The application class also contains the main method of our application.

Code for the Application.class

When you create a class that contains a main method calling the run method of the SpringApplication class, your Spring Boot application is actually ready to be started. You can try it out by running the Application (or however you named it) class as a java application.

Next, to verify that we actually have set things up properly, we’re going to create a simple REST controller that is going to respond to a single GET request and greet the user with a friendly message.

Create a package, under the package you created above, and name it “controller”. Creating the packages is not a requirement for this tutorial, but I think it’s very important for a developer to follow some basic rules of structuring an application.

Create a class – SimpleGreetingController and annotate it with the following annotations – @RestController and @RequestMapping. The @RestController annotation says to Spring that we want our class to be a Spring component and also return an HTTP body by default from its REST methods. The @RequestMapping annotation simply maps our class to the path we’re going to pass inside the annotation, so add this to the annotation – @RequestMapping(“/greeting”). This means that this controller class is responsible for all HTTP requests that call for this specific path “/greeting”.

The next thing we need is a public method that is going to respond to a GET request from a user.

Create a method – sayHello() and annotate it with @GetMapping. What this annotation does is to mark this method as a handler method for requests of type GET. We need some additional further configurations for our greeting method. As in the @RequestMapping annotation, we’re going to give the @GetMapping annotation a path as well. If you omit the path from the @GetMapping annotation, the method will use the top most path, in this case this will be “/greeting” as this is the “main” controller path. There certainly are cases when you don’t need a specific path for your methods, for example when a controller has only one of each HTTP verbs, but more on this in another tutorial.

The last configuration for our method is going to be a return type – we need to tell Spring what type of content we want to return and since we’re just returning a simple message, we’re going to use JSON.

@GetMapping(value = “/hi”, produces = MediaType.APPLICATION_JSON_VALUE)

Code for the SimpleGreetingController.java

This is all there is to start a simple web application with Spring Boot. For the application to start, you need to run the Application class (or the class containing your main method).

In the console you’re going to see messages related to the Spring Boot application startup and also the REST controller you just created. If everything is okay, you can check your new endpoint at

http://localhost:8080/greeting/hi

You should receive our friendly greeting saying that the application is working properly.

About the author

Daniel Vladimirov

Software engineer and founder of simplyprogram.com