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…
Category: Spring
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…
How to Update a Table using Sping Repository Modifying Query
You can use Spring Data JPA’s @ Modifying and @ Query annotations to achieve Update a field of a Table using Sping Repository Modifying Query. Spring Repository Here is an example: import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.transaction.annotation.Transactional; public interface UserRequestRepository extends CrudRepository<UserRequest, Long> { @Modifying @Query(“UPDATE UserRequest u SET u.fieldCount = 0”) void…
How to check every request in spring boot & if it is invalid – return 401
How to check for presence of specific header in every request with spring boot and if it is not – return 401? You can achieve this by implementing a custom HandlerInterceptor in Spring Boot. Here’s an example: import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; //import javax.servlet.http.HttpServletRequest; //import javax.servlet.http.HttpServletResponse; import java.util.Arrays; @Component public…
IOException: Stream Closed – when having ResponseEntity – a file
The error java.io.IOException: Stream Closed indicates that the InputStream passed to the InputStreamResource has already been closed before it is read by the Spring framework to create the response. Use Case The use case where you may need to encounter this error is file download from Spring Boot App. Writing down bytes loaded in memory…
How to implement partial commits in a Spring Boot service when using spring repositories
Implementing checkpoint or partial commits within a database transaction in a Spring Boot service can be tricky since Spring transactions are designed to be atomic: they either fully commit or fully roll back. However, you can achieve a similar effect by managing transactions explicitly in your service layer and leveraging Spring’s PlatformTransactionManager or nested transactions…
How to Execute something after a Spring Bean is Initialized
You can use several methods to Execute something after a Spring Bean is Initialized but the most popular one is the @PostConstruct annotation. Here’s an example: import org.springframework.stereotype.Component; @Component public class MyBean { @PostConstruct public void init() { System.out.println(“My bean is initialized”); // Your code here } } In this example, The Spring Engine will…
Allow Spring Boot Endpoint to be called only from Internal Network
You can achieve an Spring Boot Endpoint to be called only from Internal Network by using the built-in support for security and configuring a filter to check the IP address of incoming requests. Here are the steps: Step 1: Add dependencies In your pom.xml file (if you’re using Maven) or build.gradle file (if you’re using…
How to add SVG to Vaadin
Vaadin is a popular Java framework for building Web Applications, so it is logical to have the ability to add SVG to the Vaadin Web App. Method 1: Using Vaadin’s built-in SVG support Vaadin provides a built-in way to display SVGs using the SVG class. Here’s an example: import com.vaadin.flow.component.html.Div; import com.vaadin.flow.server.VaadinRequest; public class MyUI…
What could be the reason for Spring Repository cannot Delete Record
There are several reasons why a Spring Repository might not be able to Delete a Record. Here are some possible causes: No Transaction Spring Data JPA uses transactions by default, but if you’re not using a transaction manager or the @Transactional annotation, the delete operation might not be committed. Incorrect Query Make sure that the…
