Accepting a list of IDs in your Spring Boot application can be helpful when you need to fetch multiple records based on a list of IDs sent in a request. In this blog post, we will learn to accept list of ids as request parameters in spring boot. Following is a step-by-step guide to implement a solution with a controller, service, repository layer, and test data.
Step 1: Set Up the Project
Assuming you already have a Spring Boot application set up, add the necessary dependencies for Spring Data JPA and H2 (for an in-memory database).
<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>
Step 2: Define the Entity Class
Let’s create an Item
entity to represent items in a database.
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// Constructors, Getters, and Setters
public Item() {}
public Item(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Step 3: Create the Repository Layer
The ItemRepository
interface extends JpaRepository
, enabling standard CRUD operations. We will add a method to find all items by a list of IDs.
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface ItemRepository extends JpaRepository<Item, Long> {
List<Item> findByIdIn(List<Long> ids);
}
Step 4: Create the Service Layer
The ItemService
class contains the business logic. It uses the repository to fetch items based on the provided IDs.
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ItemService {
private final ItemRepository itemRepository;
public ItemService(ItemRepository itemRepository) {
this.itemRepository = itemRepository;
}
public List<Item> getItemsByIds(List<Long> ids) {
return itemRepository.findByIdIn(ids);
}
}
Step 5: Implement the Controller Layer
In the controller, we accept the list of IDs as a request parameter in a REST endpoint.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class ItemController {
private final ItemService itemService;
public ItemController(ItemService itemService) {
this.itemService = itemService;
}
@GetMapping("/items")
public List<Item> getItemsByIds(@RequestParam List<Long> ids) {
return itemService.getItemsByIds(ids);
}
}
With this setup, you can retrieve a list of items by sending a GET request with a list of IDs as a parameter.
Step 6: Insert Test Data
Use an import.sql
file to insert some test data into the in-memory H2 database when the application starts.
Create a file named import.sql
in the src/main/resources
directory:
INSERT INTO item (id, name) VALUES (1, 'Item A');
INSERT INTO item (id, name) VALUES (2, 'Item B');
INSERT INTO item (id, name) VALUES (3, 'Item C');
INSERT INTO item (id, name) VALUES (4, 'Item D');
Step 7: Test the Endpoint
Start your application and test the endpoint by sending a GET request with a list of IDs:
http://localhost:8080/items?ids=1,2,3
Expected Response
[
{ "id": 1, "name": "Item A" },
{ "id": 2, "name": "Item B" },
{ "id": 3, "name": "Item C" }
]
Summary
In this guide, we covered how to create a Spring Boot application that accepts a list of IDs as a request parameter. With this setup, you can easily retrieve multiple records from the database by passing a list of IDs in a single request. This approach helps make your API endpoints more flexible and efficient.