Spring Boot – Hello World Example

Spring Boot is developed by Pivotal Software to make the applications development easier. With Spring Boot we can create stand-alone, production-grade applications easily with minimum configuration. In this post, let’s see how to build a simple REST API endpoint using the Spring Boot.

Create the project structure

To create the project structure, let’s make use of Spring Initializr.

What you will need to build the application

  1. Java 1.8
  2. Maven
  3. Any IDE of your choice

Give the project name and artifact details to the Spring Initializr to create the project structure for building the application.

Note: If you have Jdk version other than 1.8 in your system, then choose the same version in the Spring Initializr console.

Building the sample application

We will build a simple REST service which should be able to handle GET requests for resource path /simpletask and return a 200 OK JSON response.

Sample response to expect

Create a model class

Let’s create a model class to represent the Task which will returned as JSON response.

Create a REST controller

As a next step, let’s create a controller class to handle the GET requests. Spring framework provides a handy @RestControllerannotation which marks the class as a controller for handling the HTTP requests. Below listing shows the controller class to handle the requests for /simpletask.

The @RequestParam annotation binds the query string parameter in request URL to the taskName parameter in simpleTask() method. Also if you notice the response object Task is converted to JSON  automatically using the Jackson library present in the classpath.

Build an executable JAR

The Spring Boot application can be packaged as an executable JAR file with Java main() method acting as an entry point.

The @SpringBootApplication annotation will add the following features implicitly.

  • @EnableAutoConfiguration instructs the Spring Boot to start adding/creating beans based on the classpath and other property settings.
  • @ComponentScan helps Spring to look up the components and services in the base package and to make the appropriate controller to be available for handling the requests.

Since we have used Maven to create this sample application, issue the following commands to package and execute the application.

Traverse to the project folder and issue the below command to package the application as executable JAR.

After packaging the application, execute it as below.

Alternatively you can use mvnw spring-boot:run which does both packaging and executing the application in a single step.

Test the Application

Access the service at http://localhost:8080/simpletask and you should see the below output.

Now access the service by providing the query parameter string name as http://localhost:8080/simpletask?name=SpringBootREST. The output should be as below.

Since we have configured the controller to handle the query parameter and to increment the taskId for every request, the taskId is changed from 1 to 2 and taskName is changed to Simple SpringBootREST Task!

Conculsion

We have successfully built a simple REST service using Spring Boot. We have learned how to make use of Spring Initializr to create project structure, using controller to handle the web requests, package and execute the application.

In the upcoming posts we will add more features to our application and learn more about Spring Boot. Stay tuned!
If you have any questions, feel free to post it in the comments section. Thank you!


Download source code : sping-boot-example.zip


About Vignesh M

Java developer , AWS Certified Solutions Architect Associate and Cloud technology enthusiast. Currently working for Hexaware technologies. He believes that knowledge increases by sharing not by saving.

Leave a Reply

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