Module 4:Web Services ………………Topic 3:Creating RESTful Services

Sujatha Mudadla
3 min readJul 11, 2024

--

Creating RESTful Services with Spring Boot

RESTful web services are a fundamental part of modern web applications and microservices architectures. They provide a standardized way to enable communication between different systems over HTTP. Spring Boot, combined with Spring Web MVC (also known as Spring REST), simplifies the development of RESTful web services. Let’s explore the process of creating RESTful services with Spring Boot in detail.

Introduction to RESTful Services

REST (Representational State Transfer) is an architectural style that uses standard HTTP methods to interact with resources identified by URIs (Uniform Resource Identifiers). RESTful services are stateless, scalable, and can use multiple data formats, with JSON being the most common.Key Characteristics of RESTful Services:

  1. Stateless: Each request from a client to a server must contain all the information needed to understand and process the request.
  2. Resource-Based: Resources are identified by URIs, and interactions with these resources are performed using standard HTTP methods (GET, POST, PUT, DELETE).
  3. Representation: Resources can have multiple representations, such as JSON, XML, or HTML.

Setting Up a Spring Boot Project

To create a RESTful service with Spring Boot, you need to set up a Spring Boot project. You can use Spring Initializr (https://start.spring.io/) to generate a project with the necessary dependencies.Dependencies:

  • Spring Web: For building web applications, including RESTful services.
  • Spring Boot DevTools: For development and testing.
  • Spring Data JPA: For database access (optional, if you need to interact with a database).
  • H2 Database: An in-memory database for testing (optional).

Creating a RESTful Controller

A RESTful controller in Spring Boot is a class annotated with @RestController. This annotation indicates that the class will handle HTTP requests and return responses in a RESTful manner.Example:

package com.example.demo.controller;

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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.model.User;
import com.example.demo.service.UserService;

import java.util.List;

@RestController
@RequestMapping(“/api/users”)
public class UserController {

private final UserService userService;

public UserController(UserService userService) {
this.userService = userService;
}

@GetMapping
public List<User> getAllUsers() {
return userService.getAllUsers();
}

@GetMapping(“/{id}”)
public User getUserById(@PathVariable Long id) {
return userService.getUserById(id);
}

@PostMapping
public User createUser(@RequestBody User user) {
return userService.createUser(user);
}
}

In this example, the UserController class handles HTTP requests for user-related operations. It uses @GetMapping for retrieving users and @PostMapping for creating a new user.

Service Layer

The service layer contains the business logic of the application. It interacts with the repository layer to perform CRUD operations.Example:

package com.example.demo.service;

import com.example.demo.model.User;
import com.example.demo.repository.UserRepository;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

private final UserRepository userRepository;

public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}

public List<User> getAllUsers() {
return userRepository.findAll();
}

public User getUserById(Long id) {
return userRepository.findById(id).orElse(null);
}

public User createUser(User user) {
return userRepository.save(user);
}
}

Repository Layer

The repository layer interacts with the database. Spring Data JPA provides a convenient way to create repositories by extending the JpaRepository interface.Example:

package com.example.demo.repository;

import com.example.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
}

Running the Application

To run the Spring Boot application, use the main method in the @SpringBootApplication annotated class.Example:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

Testing the RESTful Service

You can test the RESTful service using tools like Postman or curl. For example, to retrieve all users, you can send a GET request to http://localhost:8080/api/users.

Conclusion

Creating RESTful services with Spring Boot is straightforward and efficient. By leveraging Spring Boot’s features and annotations, developers can quickly build scalable and maintainable RESTful APIs.

--

--

Sujatha Mudadla

M.Tech(Computer Science),B.Tech (Computer Science) I scored GATE in Computer Science with 96 percentile.Mobile Developer and Data Scientist.