Spring Boot

Spring boot hello world example

The Spring Boot is an open-source framework that is maintained by Pivotal. It provides Java developers, a framework to begin with a production-grade auto-configurable Spring application.

It also enables the developers to rapidly start work and focus on solving business problems rather than wasting time in configuring their Spring application.

Spring Boot is also 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.datasource.url = jdbc: mysql: // localhost / test
spring.datasource.username = dbuser
spring.datasource.password = dbpass

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.

package com.codedelay.helloworld.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
@Table(name = "bicycle_table")
public class Bicycle {
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id")
	private Integer bicycleId;
	@Column(name = "brand_name")
	private String brandName;
	@Column(name = "model_name")
	private String modelName;
	private Double price;
	public Integer getBicycleId() {
		return bicycleId;
	public void setBicycleId(Integer bicycleId) {
		this.bicycleId = bicycleId;
	public String getBrandName() {
		return brandName;
	public void setBrandName(String brandName) {
		this.brandName = brandName;
	public String getModelName() {
		return modelName;
	public void setModelName(String modelName) {
		this.modelName = modelName;
	public Double getPrice() {
		return price;
	public void setPrice(Double price) {
		this.price = price;

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

package com.codedelay.helloworld.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.codedelay.helloworld.entity.Bicycle;
import com.codedelay.helloworld.service.BicycleService;
public class BicycleController {
	private BicycleService mService;
	@PostMapping(value = "/add")
	public Bicycle addBicycle(@RequestBody Bicycle bicycle) {
		return mService.addBicycle(bicycle);
	@GetMapping(value = "/get/{bicycleId}")
	public Bicycle findBicycleByID(@PathVariable("bicycleId") Integer bicycleId) {
		return mService.findBicycleByID(bicycleId);
	@PutMapping(value = "/update/{bicycleId}/{price:.+}")
	public Bicycle updateBicyclePrice(@PathVariable("bicycleId") Integer bicycleId,
			@PathVariable("price") double updatedPrice) {
		return mService.updateBicyclePrice(bicycleId, updatedPrice);
	@DeleteMapping(value = "/delete/{bicycleId}")
	public void deleteBicycle(@PathVariable("bicycleId") Integer bicycleId) {

Step – 09: BicycleService.java code

package com.codedelay.helloworld.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.codedelay.helloworld.dao.BicycleRepository;
import com.codedelay.helloworld.entity.Bicycle;
public class BicycleService {
	private BicycleRepository mRepository;
	public Bicycle addBicycle(Bicycle bicycle) {
		return mRepository.save(bicycle);
	public Bicycle findBicycleByID(Integer bicycleId) {
		return mRepository.findById(bicycleId).get();
	public Bicycle updateBicyclePrice(Integer bicycleId, double updatedPrice) {
		Bicycle temp =  mRepository.findById(bicycleId).get();
		return mRepository.save(temp);
	public void deleteBicycle(Integer bicycleId) {

Step – 10: Bicyclerepository.java

package com.codedelay.helloworld.dao;
import org.springframework.data.repository.CrudRepository;
import com.codedelay.helloworld.entity.Bicycle;
public interface BicycleRepository extends CrudRepository<Bicycle, Integer> {

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

2019-07-25 11:32:06.262  INFO 15928 --- [           main] c.c.h.SpringbootHelloWorldApplication    : Starting SpringbootHelloWorldApplication on W108W438S2 with PID 15928 (C:\Codedelay\springboot-hello-world\springboot-hello-world\target\classes started by Arpit_Nanavati in C:\Codedelay\springboot-hello-world\springboot-hello-world)
2019-07-25 11:32:06.265  INFO 15928 --- [           main] c.c.h.SpringbootHelloWorldApplication    : No active profile set, falling back to default profiles: default
2019-07-25 11:32:07.318  INFO 15928 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-07-25 11:32:07.454  INFO 15928 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 119ms. Found 1 repository interfaces.
2019-07-25 11:32:08.007  INFO 15928 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$b9ae83d9] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-07-25 11:32:08.897  INFO 15928 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-07-25 11:32:08.947  INFO 15928 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-07-25 11:32:08.947  INFO 15928 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-07-25 11:32:09.247  INFO 15928 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-07-25 11:32:09.248  INFO 15928 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2911 ms
2019-07-25 11:32:09.472  INFO 15928 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-07-25 11:32:09.739  INFO 15928 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-07-25 11:32:09.876  INFO 15928 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
	name: default
2019-07-25 11:32:10.016  INFO 15928 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.10.Final}
2019-07-25 11:32:10.019  INFO 15928 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2019-07-25 11:32:10.290  INFO 15928 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-07-25 11:32:10.549  INFO 15928 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2019-07-25 11:32:11.477  INFO 15928 --- [           main] o.h.t.schema.internal.SchemaCreatorImpl  : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@1f60824e'
2019-07-25 11:32:11.481  INFO 15928 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-07-25 11:32:12.158  INFO 15928 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-07-25 11:32:12.233  WARN 15928 --- [           main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2019-07-25 11:32:12.614  INFO 15928 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-07-25 11:32:12.619  INFO 15928 --- [           main] c.c.h.SpringbootHelloWorldApplication    : Started SpringbootHelloWorldApplication in 6.907 seconds (JVM running for 7.583)


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.

Click to rate this post!
[Total: 0 Average: 0]

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.