Category: Spring Boot

  • How to create a custom Health Check in Spring Boot for Microservices?

    Let’s say you have three microservices in your application. One of the microservices connects to the other two microservices. And in addition it also connects to a database. You want to check the health status of this microservice and its dependent microservices and database. How to do this in Spring Boot? Spring Boot provides an…

  • How to deal with optional request parameters in Spring Boot?

    Let’s say you are creating a REST API in Spring Boot which accepts request parameters. You use the annotation @RequestParam to retrieve the request values. And if user does not supply the request parameter Spring will throw error. What if the request parameter is just an optional parameter? How to handle this in Spring ?…

  • How to download a Microsoft Word document from a Spring Boot REST API?

    Let’s say you want to create a REST API which returns a Microsoft Word Document. When a client calls that API the document gets automatically downloaded to their system. How do you do it? Here is the algorithm: STEP1: Create a REST API which returns a byte array STEP2: Configure the return type of the…

  • How to schedule tasks in Spring Boot?

    Let’s say you want to schedule background tasks in your application , may be every ten minutes or at a fixed time every day or during weekends. How do you do that in Spring Boot? Spring Boot provides a simple and straightforward solution: By just using @Scheduled annotation. Here are the steps in detail: STEP1:…

  • How to read a JSON request inside a Spring Boot filter?

    Let’s say you are using a filter in your spring boot application. Spring boot (Spring Security) already internally uses many filters to filter requests coming to your application. If you are going to create a custom filter you can do so by implementing Filter interface from javax servlet package or by extending GenericFilterBean/ OncePerRequestFilter provided…

  • How to consume a large response using Spring WebClient?

    Spring WebClient is a new library provided by Spring to call REST services. It can be used to call rest services in an asynchronous way. Spring team is planning to retire RestTemplate and so it is better to start using Spring WebClients in your projects even for synchronous calls. And if you do use Spring…

  • What is @MapsId used for in JPA/hibernate? – Part 1

    @MapsId annotation. If you ever wondered what this annotation is for , here is an in depth analysis of it: When is this annotation used? It is used if the primary key of a child table is the same as the primary key of a parent table Why is this used? By specifying the annotation…

  • How to call a REST API protected with SSL (https) from Spring Boot without importing the certificate into java keystore ?

    In the previous post we saw how to consume a REST API protected with SSL (HTTPS) by importing necessary SSL certificates into JVM keystore That serves fine if you have access to the JVM . In case if you don’t and want to bundle those certificates along with your application and use it to call…

  • How to call REST API protected with SSL (https) from Spring Boot ?

    Let’s say you want to invoke a REST API from your spring boot application. And it is protected with SSL. In other words you need an SSL certificate to access that application, else you won’t be given access to it. Your java keystore already has a lot of inbuilt certificates(In my local it is present…

  • How to create a custom annotation in Spring Boot?

    How to create a custom annotation in Spring Boot?

    Annotation is a powerful feature in Java. You can plug in some logic by just adding a word ! Consider this annotation in Spring Data: @Transactional Add this to a method where you are performing a sequence of database operations and they suddenly turn transactional. If any of the database operation fails all the other…