Tag: springboot
-
How to trace logs across distributed applications/microservices in Spring Boot?
Logging is so critical to applications. Good logging strategy can help you fix issues in production environment quickly and help you save time and money. With the adoption of microservices , proper logging has got even more importance. One of the difficulties in debugging microservices is that the flow of API requests are spread across…
-
How to retrieve URL and Query parameters in Spring Boot?
Let’s say you want to create a Spring Boot REST API which accepts URL and query parameters. How do you retrieve those parameters in the code ? By using @RequestParam annotation for query parameter @PathVariable annotation for URL parameter Here is an example: The above method is a GET REST service which takes in a…
-
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 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 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…
-
How to run environment specific code in Spring Boot?
Let’s say you want to run environment specific code in your Spring Boot application. Say if the environment is local , you get data from a temporary cache and if the environment is anything higher you get it from a third party service. How to do this? You can specify the environment to be activated…
-
How to do user authentication in Spring Boot using PostgreSQL database?
Let’s say you want to create an application in Spring Boot and authenticate the users who use it against credentials stored in a PostgreSQL database. Below are the high level steps to implement it in Spring Boot: Create a spring boot project with authentication related dependencies. Add the database configuration in application.properties against which you…