Hackforge Academy

Category: spring_boot

Search filter APIs

Published on 26 Jun 2026

Explanation

Search APIs allow clients to find records based on specific criteria such as name, email, department, or keyword. Spring Boot and Spring Data JPA provide multiple approaches to implement search functionality efficiently.

Code:

@Entity
public class Student {

    @Id
    @GeneratedValue(strategy = 
GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String email;
    private String department;
}

Explanation

Spring Data JPA can generate search queries automatically using method naming conventions. The 'Containing' keyword is commonly used for partial text searches.

Code:

public interface StudentRepository 
extends JpaRepository<Student, Long> {

    List<Student> 
findByNameContaining(String keyword);
}

Explanation

A REST endpoint can accept search keywords through query parameters and return matching records from the database.

Code:

@GetMapping("/students/search")
public List<Student> searchStudents(
        @RequestParam String keyword) {

    return studentRepository.
findByNameContaining(keyword);
}

Explanation

Custom JPQL queries provide more control when searching across multiple fields. This is useful when users need to search by name, email, or department using a single keyword.

Code:

@Query("SELECT s FROM Student s WHERE " +
       "LOWER(s.name) LIKE 
LOWER(CONCAT('%', :keyword, '%')) OR " +
       "LOWER(s.email) 
LIKE LOWER(CONCAT('%', :keyword, '%'))")
List<Student> searchByKeyword(
@Param("keyword") String keyword);

Explanation

Search APIs often include pagination and sorting to improve performance and user experience. This prevents large result sets from being returned in a single response.

Code:

@GetMapping("/students/search")
public Page<Student> searchStudents(
        @RequestParam String keyword,
        @RequestParam int page,
        @RequestParam int size) {

    Pageable pageable = 
PageRequest.of(page, size);
    return studentRepository.
findByNameContaining(keyword, pageable);
}

๐Ÿš€ Learn Spring Boot with real-world projects

๐Ÿ’ก Build REST APIs step by step

๐Ÿง  Improve backend development skills

๐ŸŽฏ Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

๐Ÿ

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
โš›๏ธ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
โ˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community