Category: Spring Boot
-
How to automate java bean to bean mapping in Spring Boot using MapStruct?
Let’s say you are developing an application which fetches data from the database. Let’s say you are using hibernate to do the object relational data mapping. So the data you fetch from database are mapped into Java entity objects . And then you extract data from this entity object into another java bean object (DTO/data…
-
How to group related attributes together while doing hibernate entity mapping?
Let’s say you have chosen Hibernate ORM to save data to your database. For each table you have written a corresponding entity class in your java application. And then while doing so you start noticing that a lot of the entity classes have few fields repeated. You wonder if it would be nice to keep…
-
How to automate database migration in Spring Boot using flywaydb?
Let’s say you want to migrate your database to another environment. For example , you have your database set up in development environment and you want to migrate that to test environment. The traditional way to do that is to create scripts for each database change , store them in a file and fire them…
-
How to enable CORS (Cross Origin Resource Sharing) for specific domains in Spring Boot?
For security reasons, browsers don’t allow javascript code to call APIs deployed in domains other than the current domain in which the javascript code is deployed. So if your javascript code is in a.com you cannot call an API deployed in b.com. So if you have deployed an API developed in Spring Boot in b.com…
-
How to convert your Spring Boot app to a HTTPS application?
Let’s say you have a simple spring boot app with a single API which returns the string “Hello HTTPS”. But this runs on HTTP and you want to convert this to run on HTTPS. You want your app to be more secure and the data sent over the network to be encrypted. You can do…
-
How to redirect to an external URL from Spring Boot REST Controller (Post/Redirect/Get pattern)?
Let’s say you want to redirect users to an external URL when they make an API request. How to do this in Spring Boot? There are two ways you can do this. One using ResponseEntity object Two using RedirectView object. Here is how to do it with ResponseEntity object: STEP1: Create a REST Controller which…
-
How to interpret and modify REST requests in Spring Boot in a centralized way?
Let’s say you have created a bunch of REST services in your Spring Boot app. And you want to perform a common action on all the requests. May be you want to log the incoming requests to a log file. Instead of doing this action for every REST service you can do it in a…
-
How to trace distributed logs using Spring Cloud Sleuth?
In the previous post, I explained how to trace logs across distributed applications/microservices using MDC design pattern provided by all major logging frameworks like slf4j. Spring Boot has made it even easier. You just need to include the dependency spring-cloud-sleuth and do some minor changes and your logs are automatically appended with tracing ids .The…
-
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…