Spring boot rest example

In this tutorial, we will see how to create Restful web services using Spring boot.
There are not many differences between creating Rest API using Spring Rest JSON which we have already seen earlier. Spring Boot just makes easier to Rest API. You don’t have to provide any additional JSON dependency in the classpath. Spring Boot will take care of all the dependencies required to create Rest API.
Here are steps to create a simple Spring boot rest example.

Spring Boot Rest example:

Github Source code:

Project structure:

Spring Boot Rest Project Structure

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Step 1:  Create a dynamic web project using maven in eclipse named "SpringBootRestAPIExample".

Step 2:  Now change pom.xml as follows:

Step 3: Create a bean name "Country.java" in org.arpit.java2blog.springboot.bean.

Create controller

Step 4:  Create a controller named "CountryController.java" in package org.arpit.java2blog.springboot

b>@PathVariable: Used to inject values from the URL into a method parameter.This way you inject id in getCountryById method.

Spring application configuration:

Step 5: Create a package named "org.arpit.java2blog"
create a class named "SpringBootHelloWorldApplication.java"

We have just added @SpringBootApplication and it does all the work.
Let’s understand more about this annotation.
strong>@SpringBootApplication is an annotation that adds all of the following:

strong>@Configuration makes the class as a source of bean definitions for the application context.
strong>@EnableAutoConfiguration enables Spring boot to add beans presents in classpath setting and various property setting.
Normally you would add
strong>@EnableWebMvc for a Spring MVC application, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath.
This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.
strong>@ComponentScan tells Spring to look for other components, configurations, and services in the default package, allowing it to find the controllers.
If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

Step 6: It ‘s time to do maven build.

Right click on project -> Run as -> Maven build
Step 7: Provide goals as clean install (given below) and click on run.
 Spring Boot Rest Maven build
Step 8: Test your REST service under: “http://localhost:8080/countries”.

You will get following output:

Spring Boot Rest get All Countries

Step 9: Now pass country id as parameter to url.
Spring Boot Rest Country ID
That’s all about Spring boot rest example. If you find any issues, please comment.


You may also like:


You may also like:

Related Posts

  • 11 January

    Spring boot JDBC example

    In this post, we will see Spring boot JDBC example. As we already know Spring boot does lot of auto configurations which help us to avoid a lot of boilerplate code. In the case of JDBC, spring boot provides auto-configuration such as creating DataSource and JDBCTemplate objects automatically based on application.properties. So you just need […]

  • 02 January

    Spring boot devtools tutorial

    In this post, we’ll be looking up what does spring has to offer for developer experience. Overview Using IDEs like Eclipse, Spring Tool Suite, Intellij Idea a typical development setup includes an application code base that gets deployed on a Web Application Server like Wildfly or Tomcat or even Glassfish. The speed of development is […]

  • 26 December

    Spring boot H2 Database example

    In this post, we are going to see how to create Spring boot application integrating with H2 database. What is H2 database? H2 is open source database. It is very fast and smaller in size. It is in-memory database and keeps all data in memory. If you start and stop the application, all the data […]

  • 22 December

    Spring Boot CrudRepository

    In this post, we’ll be looking up how to create and use Spring Boot CrudRepository. SpringBoot CrudRepository provides sophisticated CRUD functionality for the type of entity you want to be managed.This interface acts primarily as a marker interface to capture the types to work with and to help you to discover interfaces that extend this […]

  • 10 April

    Difference between Spring and Spring boot

    In this post, we will see difference between Spring and Spring boot. If you have followed spring project over time, you might have noticed it has become increasingly complex. If you want to create a spring application, you still need to put a lot of efforts. Spring boot is introduced to save time to bootstrap […]

  • 17 September

    Spring Boot SOAP Web service Example

    In this post, we will see how to create soap web services with Spring boot. We will create contract first soap web service with Spring boot. We will focus on how to define configurations for soap web services. Tools used JDK 1.8, Eclipse, Maven Spring-boot – Underlying application framework wsdl4j – for publishing WSDL for our […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.