Category: Spring Boot
-
How to pass URL and query parameters in Spring REST client?
Let’s say you are making a GET request to a third party API from your spring application. You want to pass both URL and query parameters. First, what are URL and query parameters? URL parameters: URL parameters are passed along with the URL like this: https://thirdpartyapi.com/request/1 In the above request , let’s assume the number…
-
How to retry service calls in Spring Boot ?
Let’s say you are building a spring boot app which calls other third party REST APIs. And those APIs are not very reliable. They throw error at times but works fine most of the times. So you want to retry hitting them when they error out so that you are chance of getting a response…
-
How to log incoming requests to all REST services in Spring Boot?
(As an alternate to the below solution you can check out this post , which is easier and more straightforward) Let’s say you want to log all the requests to your REST APIs developed using Spring Boot in a centralized way. You don’t want to add logs to each and every API. How to do…
-
How to save both parent and child records on saving parent record in JPA/hibernate?
Let’s say you have created an application to post blogs. You are allowing users to comment on the blogs. Every blog is associated with many comments. You have chosen spring data as your backend technology. How would you design this ? You can create an entity class representing a blog post , say Blogpost.java And…
-
How to log IP addresses of incoming requests to a Spring Boot app in a centralized way?
Let’s say you want to track the source of hits coming to your REST services. And you have developed them using Spring Boot. Here is the algorithm: STEP1: Create a Spring Handler Interceptor STEP2: Retrieve the IP address in the prehandle method of the Handler interceptor. 2.a) Use X-Forwarded-For header extracted from HttpRequest object to…
-
How to send a custom error message for bad requests (400 error code) in Spring Boot?
Let’s say you created a REST API which accepts a JSON input. You handle any type of exception in your code and throw a custom error message to the user. But what if the user sends a bad JSON ? Spring framework will throw a predefined error message. Below is the error message I got…
-
How to restart your Spring Boot application automatically for every code change?
One factor which slows down developer’s productivity is restarting the application for every code change to reflect. And even start up time takes time. The time is much less in Spring Boot compared to traditional MVC applications. And new frameworks like Micronaut even have faster start up times. To avoid the manual restart , Spring…
-
How to use Spring WebClient to invoke REST services reactively and non reactively?
Let’s say you want to invoke a reactive REST service developed using Spring WebFlux. A reactive REST service is one which lets you invoke itself asynchronously. You either get a Mono response (for a single object response) or a Flux response (for multiple objects). Here is a demo of how to call a reactive REST…
-
How to create a reactive REST service using Spring WebFlux?
Reactive programming is gaining more popularity now than ever before. Traditionally when you invoke a REST service it becomes a blocking call. You need to wait until you get the response and then proceed with your next line of code. With reactive programming , you can make an asynchronous call and then retrieve the response…
-
How to set read time out for calling REST services using Spring REST template ?
Let’s say you are invoking a REST service using Spring’s REST template. And you want to set the read time out to a certain value. You don’t want the invoked service to take too much time to send data. You can do this in two ways: Set the time out locally Set the time out…