To Optimize backend endpoints in a Spring Boot app with Java 17 can significantly improve performance and scalability. Here’s a structured list of strategies, starting from database-level optimizations and moving to application and infrastructure layers: Database/SQL Optimization ✅ Optimize SQL Queries Use EXPLAIN plans to analyze slow queries. Use proper indexing on columns used in…
Category: Spring
Spring Boot CSV Export
Below is a Spring Boot example that demonstrates Export of data to CSV by processing records in two ways with and without library. A count() query to get the total number of records. Paging (PageRequest) to fetch records batch-by-batch. Writing directly to the response’s OutputStream to avoid holding all records in memory. ✅ Assumptions You…
How to Generate an Excel File and Download it in Vaadin Web App
To generate and download an Excel file in a Vaadin web application (e.g., Vaadin 23+ with Java backend), you can use Apache POI to create the Excel file and Vaadin’s StreamResource to serve it for download. ✅ Steps: Add Apache POI to your pom.xml: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.4.1</version> </dependency> Create a Vaadin download button that…
Offset of type Long in Java Object Relational Mapping Frameworks
To this day amazes me that all Java Object Relational Mapping Frameworks use 32bit Integer as parameter to the offset you could pass to the database. Spring’s Pageable uses int internally — so it cannot handle offsets beyond Integer.MAX_VALUE. (JPA API also expects int for offset and limit. https://docs.oracle.com/javaee/5/api/javax/persistence/Query.html#setFirstResult(int) Hibrnate https://docs.jboss.org/hibernate/orm/6.6/javadocs/org/hibernate/query/Page.html#page(int,int) 🔍 Breakdown: 1. Pageable…
How to pass a List in Spring Repository Query
To pass a List (or any Collection) into a Spring Data JPA repository query, you can use the IN clause in JPQL or native SQL, and Spring will automatically bind the list elements. ✅ Example 1: Using @Query with JPQL @Query(“SELECT p FROM Product p WHERE p.id IN :ids”) List<Product> findByIds(@Param(“ids”) List<Long> ids); You can…
How to return HashMap from Spring Repository
A Spring Data Repository typically returns entities or List of Object arrays List<Object[]> – for custom queries, not collections like HashMap<K, V> directly. You can structure a custom query or a method that returns a Map in your Service or in additional method, but it’s not automatic — you have to be explicit about it….
Spring Boot RestController output fromat
Spring Boot decides which format (JSON, XML, Protobuf, etc.) to use when handling a request based on the configuration for content negotiation. 1. Content Negotiation in Spring Boot Spring Boot decides how to serialize and deserialize request/response bodies using HttpMessageConverters. These converters are automatically registered based on the libraries in the classpath. 2. How Does…
How to Optimize an Inefficient N+1 Query Problem in Spring Boot
The N+1 query problem is a common issue that occurs when using JPA repositories in Spring Boot. It happens when you have a one-to-many or many-to-one relationship between entities, and you’re fetching the parent entity with its children. This results in a separate database query for each child, leading to inefficient performance. Optimizing N+1 Query…
Object Mapping – examples With Java Spring Boot
There are several Java Frameworks available that does Object Mapping – automatic creation of DTO (Data Transfer Object) in Spring Boot. Reasons we need it Spring (Boot) Framework saves a lot of time of developers by hiding complexity and placing smart defaults with convention over configuration. This comes with the price of a “magic” happening…
Database Integrations in Spring Boot
Here are minimal examples of different Database Integrations in Spring Boot that you could choose from in your applications. 1. JdbcTemplate (Plain JDBC) JdbcTemplate is a lower-level approach that directly executes SQL queries. import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; @Repository public class UserJdbcRepository { private final JdbcTemplate jdbcTemplate; public…