How to Build a Web Application with Java and Spring Boot

In today's digital era, building web applications has become a vital skill for developers. Among the many frameworks available, Spring Boot stands out as a powerful and developer-friendly framework for creating robust web applications. In this guide, we'll walk you through the steps to build a web application using Java and Spring Boot.

JAVAWEB DEVELOPMENT

1/14/20252 min read

Why Choose Spring Boot?

Spring Boot simplifies Java web application development by eliminating boilerplate code and configuration. With features like embedded servers, auto-configuration, and a vast ecosystem of plugins, Spring Boot is ideal for developers who want to focus on business logic instead of setup.

Prerequisites

Before we start, ensure you have the following:

  1. Java Development Kit (JDK): Install JDK 17 or later.

  2. IDE: IntelliJ IDEA, Eclipse, or Visual Studio Code with Java support.

  3. Build Tool: Maven or Gradle.

  4. Spring Boot Initializer: Access to start.spring.io.

Step 1: Set Up Your Project

  1. Go to Spring Initializr: Visit Spring Initializr.

  2. Select Project Settings:

    • Project: Maven (or Gradle)

    • Language: Java

    • Spring Boot Version: Choose the latest stable version.

    • Dependencies: Add "Spring Web" and "Spring Boot DevTools."

  3. Generate the Project: Click "Generate" to download a .zip file. Extract it to your desired location.

Step 2: Create Your First Controller

A controller handles HTTP requests and maps them to specific methods in your application.

  1. Navigate to src/main/java/com/example/demo (replace com.example.demo with your package name).

  2. Create a new file named HelloController.java and add the following code:

package com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello, Spring Boot!"; } }

  1. Start your application by running the DemoApplication class, then navigate to http://localhost:8080/hello to see your message.

Step 3: Add a Service Layer

A service layer encapsulates business logic and makes your code more modular.

  1. Create a new file named GreetingService.java in the service package:

package com.example.demo.service; import org.springframework.stereotype.Service; @Service public class GreetingService { public String getGreeting() { return "Hello, Spring Boot with a Service Layer!"; } }

  1. Modify the HelloController to use the service:

package com.example.demo; import com.example.demo.service.GreetingService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { private final GreetingService greetingService; public HelloController(GreetingService greetingService) { this.greetingService = greetingService; } @GetMapping("/hello") public String sayHello() { return greetingService.getGreeting(); } }

Step 4: Connect to a Database

Spring Boot makes it easy to integrate with databases. Here’s how:

  1. Add Dependencies: In pom.xml, add:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency>

  1. Configure Application Properties:

In src/main/resources/application.properties, add:

spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=password spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

  1. Create an Entity:

package com.example.demo.entity; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; @Entity public class User { @Id @GeneratedValue private Long id; private String name; // Getters and setters omitted for brevity }

  1. Create a Repository:

package com.example.demo.repository; import com.example.demo.entity.User; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Long> { }

Step 5: Add Frontend (Optional)

If you want to add a frontend, integrate your Spring Boot application with Angular, React, or Thymeleaf for a seamless user experience.

Conclusion

Congratulations! You've built a basic web application using Java and Spring Boot. From setting up your project to adding a database, you've covered the essential steps. Spring Boot offers a vast array of features to help you scale your application as needed.

For more tutorials, tips, and insights, keep exploring jogindrakumar.com.