CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > How to Accept List of IDs as Request Parameters in Spring Boot

Java Tutorial Menu

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

How to Accept List of IDs as Request Parameters in Spring Boot

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: November 11, 2024 Β· 3 min read Β· 0 Comments
Share: X in πŸ”—

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.

Table of Contents

  • Step 1: Set Up the Project
  • Step 2: Define the Entity Class
  • Step 3: Create the Repository Layer
  • Step 4: Create the Service Layer
  • Step 5: Implement the Controller Layer
  • Step 6: Insert Test Data
  • Step 7: Test the Endpoint
    • Expected Response
  • Summary

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.

Related Posts:

  • Most Frequently Asked Spring Boot Interview…
  • Pagination and Sorting in Spring Boot
  • Spring Data JPA Projection
  • Master Spring Data JPA Method Queries: The Ultimate…
  • Spring Data REST example.
  • JDBC Interview Questions: Ace Your Technical Screening
Tags:javaspring-bootspring-boot-data-jpa
Was this article helpful?
← Previous ArticleMaster Spring Data JPA Method Queries: The Ultimate Guide for Spring Boot Developers
Next Article β†’How to Upgrade AWS EC2 Ubuntu Version: A Step-by-Step Guide

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
Β© 2026 CoderSathi. All rights reserved. Privacy Policy Β· Sitemap