📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this tutorial, we are going to build a Search / Filter REST API using Spring Boot, Spring Data JPA, and MySQL Database.
YouTube Video
- Spring Web
- Spring Data JPA
- Lombok
- MySQL Driver
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2. Configure MySQL database
Let's use the MySQL database to store and retrieve the data in this example and we gonna use Hibernate properties to create and drop tables.
Open the application.properties file and add the following configuration to it:spring.datasource.url=jdbc:mysql://localhost:3306/search_api
spring.datasource.username=root
spring.datasource.password=Mysql@123
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
Make sure that you will create a search_api database before running the Spring boot application.Also, change the MySQL username and password as per your MySQL installation on your machine.
spring.datasource.url=jdbc:mysql://localhost:3306/search_api
spring.datasource.username=root
spring.datasource.password=Mysql@123
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
3. Create JPA Entity
package net.javaguides.springboot.entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import jakarta.persistence.*;
import java.time.LocalDateTime;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "products")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String sku;
private String name;
private String description;
private boolean active;
private String imageUrl;
@CreationTimestamp
private LocalDateTime dateCreated;
@UpdateTimestamp
private LocalDateTime dateUpdated;
}
4. Create a Repository Layer
package net.javaguides.springboot.repository;
import net.javaguides.springboot.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface ProductRepository extends JpaRepository<Product, Long> {
}
5. Create JPQL Query to Seach Products
@Query("SELECT p FROM Product p WHERE " +
"p.name LIKE CONCAT('%',:query, '%')" +
"Or p.description LIKE CONCAT('%', :query, '%')")
List<Product> searchProducts(String query);
import net.javaguides.springboot.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface ProductRepository extends JpaRepository<Product, Long> {
@Query("SELECT p FROM Product p WHERE " +
"p.name LIKE CONCAT('%',:query, '%')" +
"Or p.description LIKE CONCAT('%', :query, '%')")
List<Product> searchProducts(String query);
}
6. Create Service Layer
ProductService Interface
package net.javaguides.springboot.service;
import net.javaguides.springboot.entity.Product;
import java.util.List;
public interface ProductService {
List<Product> searchProducts(String query);
Product createProduct(Product product);
}
ProductServiceImpl Class
package net.javaguides.springboot.service.impl;
import net.javaguides.springboot.entity.Product;
import net.javaguides.springboot.repository.ProductRepository;
import net.javaguides.springboot.service.ProductService;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {
private ProductRepository productRepository;
public ProductServiceImpl(ProductRepository productRepository) {
this.productRepository = productRepository;
}
@Override
public List<Product> searchProducts(String query) {
List<Product> products = productRepository.searchProductsSQL(query);
return products;
}
@Override
public Product createProduct(Product product) {
return productRepository.save(product);
}
}
7. Create Controller Layer and Build Search REST API
package net.javaguides.springboot.controller;
import net.javaguides.springboot.entity.Product;
import net.javaguides.springboot.service.ProductService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/v1/products")
public class ProductController {
private ProductService productService;
public ProductController(ProductService productService) {
this.productService = productService;
}
@GetMapping("/search")
public ResponseEntity<List<Product>> searchProducts(@RequestParam("query") String query){
return ResponseEntity.ok(productService.searchProducts(query));
}
@PostMapping
public Product createProduct(@RequestBody Product product){
return productService.createProduct(product);
}
}
Search REST API:
@GetMapping("/search")
public ResponseEntity<List<Product>> searchProducts(@RequestParam("query") String query){
return ResponseEntity.ok(productService.searchProducts(query));
}
Comments
Post a Comment
Leave Comment