Spring boot hello world example

Spring Boot is an opinionated version of the Spring framework that uses autoconfiguration to speed up the development. 

Spring boot is opinionated which means it automatically add all required dependencies (jars) when you add a starter dependency in pom.xml.

Let’s take an example of spring-boot-starter-web.

When you add spring-boot-starter-web in maven dependency then spring boot will determine all jars that may be required to create a web application, download them and them to project’s classpath.

Spring boot also configures a default embedded tomcat container for you, so that you don’t have to configure a servlet container.

One another feature of spring boot is autoconfiguration.

But what is the autoconfiguration?

For example, to work with the database it is sufficient to add spring-boot-starter-data-jpa the dependency in pom.xml and add these properties to the configuration file:

Spring Boot can automatically configure a database if database jar (for example apache derby jar) is present in the project’s classpath.

Spring Boot Hello World Example

It is always a good idea to create a hello world example when you just start learning new technology.

In this hello world example, we will create CRUD APIs for a Bicycle Shop.

That means by using those APIs bicycle shop owner can add, retrieve, update, or delete bicycle from his inventory.

For this tutorial, we will use the H2 Database for database operations.

Steps for creating Hello World Example

Step – 01: Generate the new spring project from the Spring Initializr project by providing the below details.

Also, add three dependencies Spring Web Starter, H2 Database, and Spring Data JPA

Spring boot hello world example
Spring boot hello world example

Step – 02: Extract the project and import project in Eclipse or STS (Spring Tool Suite).

Step -3 Open pom.xml and verify that all dependencies are there in the pom file.

Spring boot pom.xml
Spring boot pom.xml

Step – 04: Now click on Maven -> Update Project to update project dependencies.

Also, Don’t forget to click on ‘Force Update‘.

Step- 05: Go to the src folder of the project.

You could see one default java file SpringbootHelloWorldApplication in the com.codedelay.helloworld package

This is the main file of the spring boot project.

Did you notice that the class is annotated with @SpringBootApplication annotation?.

Spring boot hello world java example
Spring Boot Main Application Class

This annotation tells spring to enable autoconfiguration.

In addition to that, it also tells spring to enable the component scan.

For more details of this annotations, you can check out my spring boot annotations tutorial.

Step – 06 Now, add an entity, service, controller, and repository packages and classes.

Step – 07 Open Bicycle.java and add properties along with JPA annotations.

As you could see Bicycle class is annotated with @Entity.

@Entity tells that Bicycle is an entity class and is mapped to a database table.

@Table(name = “bicycle_table”) specifies that Table name would be bicycle_table. If you don’t specify, Table name would be same as the class name.

You could also notice that bicycleId is annotated with GeneratedValue.

That specifies that bicycleId is a primary key, and its value will automatically generate.

Step – 08: Open BicycleController and expose CRUD rest APIs

Step – 09: BicycleService.java code

Step – 10: Bicyclerepository.java

Now run the program and verify there shouldn’t be any error

Conclusion

In this tutorial, we have learned about spring boot with an example. If you have a doubt please let me know via a comment in the comment section.

6 thoughts on “Spring boot hello world example”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.