My learning diary

Spring Boot Interceptors

A neat trick to log HTTP requests and responses in a Spring Boot project is through interceptors. To log requests and responses between the client and the server, I use the HandlerInterceptor (Introduction to Spring MVC HandlerInterceptor). To create this interceptor, simply extend the HandlerInterceptor interface. By overriding the various methods available, you can intercept the request and/or response at different points of time. For example, preHandle is executed before the request is executed (i.

Continue reading "Spring Boot Interceptors"

Numerical IDs in MongoDB

Auto-generated IDs in MongoDB are “strange” strings. I quote “strange” because they are actually derived not out of nowhere despite looking like they had nothing to do with anything. But to users, these IDs are strange. I had a collection of documents with a name property. Originally, name was annotated with @Id. But it meant that I could not change the value of name. name was also annotated with regex validation (@Pattern(.

Continue reading "Numerical IDs in MongoDB"

Abstract classes and AOP

I wanted to avoid writing boilerplate code for CRUD in Spring Boot. But I failed - I could not make my child aspect get along with my child controller. Before adopting AOP, I had an abstract controller which the child controller inherited. CRUD worked well and I no longer needed to repeat the code for simple CRUD anymore. I was happy. // MyAbstractController public abstract class MyAbstractController<T extends UniversalModel> { private final .

Continue reading "Abstract classes and AOP"

No qualifying bean

In one of my previous posts, I avoided auto-configuring primitives. Otherwise, the No qualifying bean error will occur. Today, I couldn’t avoid that anymore. I had to auto-configure a boolean so that the consumer can enable or disable a feature in application.yml. So, how did I resolve this issue? I used @Value("{someKey:someDefault}") final boolean someFlag in the caller constructor. Before that, I tried attaching only @Bean to the getter, but it didn’t work out.

Continue reading "No qualifying bean"

Almost done with the custom starter

I was right. I forgot to enable the Mongo repository. And it wasn’t only that. I had two Mongo configuration classes. One in the consumer and another in the custom starter. I needed only one. I removed the configuration class from the custom starter. Then, I enabled the custom starter’s repository in the consumer’s configuration class. Below is an idea of how my configuration class looks like: @EnableMongoRepositories(basePackages = { "yourTLD.

Continue reading "Almost done with the custom starter"