Create a web application with Spring Boot

In this tutorial you will learn how to install Spring Boot and create a web application with Spring Boot from scratch. The project will be based on REST web services (or commonly microservices).

What is Spring Boot? It is a framework or tool that helps to build applications mainly based on microservices in a more agile way.

Install Spring Boot

There are three ways:

  • Installing Spring Boot Starter (which is an Eclipse with integrated Spring Boot). It can be downloaded from its official page: https://spring.io/tools.
  • Accessing the page https://start.spring.io , generating the project and download it to your PC.
  • Installing Spring Tools Suite in Eclipse (It is installed from Eclipse, as a plugin).

In this case, I will choose the third option and install Spring Tools Suite in Eclipse, with java JDK 8. To do this:

Open Eclipse, go to Help -> Eclipse Marketplace …

Enter “sts” in the text box to search and press Enter.

Displays the Spring Tools 4 – for Spring Boot option (aka Spring Tool Suite 4). Click on the Install button.

Create a web application with Spring Boot
Install Spring Boot in Eclipse

The installation will take a few minutes and Eclipse will ask you to restart it.
Once restarted, Eclipse observes that a new button (Boot Dashboard) appears in the top menu bar.

Create project

Go to File -> New -> Project and choose Spring Boot -> Spring Boot Starter. Press Next.
Then fill in the details for the project and press Next:

Create a web application with Spring Boot
Create Project Spring Boot

Now, check the boxes for Spring Web Services and Spring Web Starter. Press Finish:

Create a web application with Spring Boot
Depending on the boxes that we activate, its dependencies will be created in the pom.xml file.

Please note that there are no errors when creating the project. Test running an application (running the project as Spring Boot App) and looking at a console output like the following:

Crear aplicación web con Spring Boot

Schedule services

In my case I have created a service that returns a list of users.

UserServiceRest.java

In this class the important thing is the annotations that we have added and in a simple way so that you understand what each one means, I explain:

@RestController -> Indicates that it will be a class that will publish REST services. 
@RequestMapping (value = "/ users", method = RequestMethod.GET) -> indicates the name of the method accessible from the browser. In this case users. And the type of method is also indicated, in this case of type GET.

Class UserServiceRest.java source code:

package es.springboot.rosamarfil.services;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import es.springboot.rosamarfil.model.User;


@RestController
public class UserServiceRest {
	
	/**
	* Lista de ejemplo de usuarios
	*/
	private static List<User> listaUsuarios = new ArrayList<User>() {
		{
			add(new User("Rosa", "Marfil"));
			add(new User("Pepito", "Grillo"));
			add(new User("Manuela", "Lago"));
		}
	};
	
	@RequestMapping(value="/users", method=RequestMethod.GET)
    public List<User>  getUsers(){
        return listaUsuarios;
    }
}

User.java

package es.springboot.rosamarfil.model;

import java.io.Serializable;

public class User implements Serializable {

	private static final long serialVersionUID = 1L;

	public String name;

	public String username;

	public User() {
		super();
	}
	
	public User(String name, String username) {
		super();
		this.name = name;
		this.username = username;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

}

Test REST service

After programming the code, start your Spring Boot application and display the result in the browser by entering in the address bar: http://localhost:8080/users

The result will be:

Crear aplicación web con Spring Boot

As you can see, with this little tutorial you have learned to create a web application with Spring Boot and publish a REST service locally.

Obviously, a real project would have many more layers and would be more complex, but the concept of REST Web Services with Spring Boot is what I have explained to you.

I hope it has been useful to you. Thanks for reading and share if you liked it. See you in the next tutorial.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *